Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:32:05+00:00 2026-05-30T01:32:05+00:00

I have such example. function Rabbit() { var jumps = yes; }; var rabbit

  • 0

I have such example.

function Rabbit() {
    var jumps = "yes";
};
var rabbit = new Rabbit();
alert(rabbit.jumps);                    // undefined
alert(Rabbit.prototype.constructor);    // outputs exactly the code of the function Rabbit();

I want to change the code in Rabbit() so that the var jumps becomes public. I do it this way:

Rabbit.prototype.constructor = function Rabbit() {
    this.jumps = "no";
};
alert(Rabbit.prototype.constructor);    // again outputs the code of function Rabbit() and with new this.jumps = "no";
var rabbit2 = new Rabbit();             // create new object with new constructor
alert(rabbit2.jumps);                   // but still outputs undefined

Why is it not possible to change the code in constructor function this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:32:07+00:00Added an answer on May 30, 2026 at 1:32 am

    You cannot change a constructor by reassigning to prototype.constructor

    What is happening is that Rabbit.prototype.constructor is a pointer to the original constructor (function Rabbit(){...}), so that users of the ‘class’ can detect the constructor from an instance. Therefore, when you try to do:

    Rabbit.prototype.constructor = function Rabbit() {
        this.jumps = "no";
    };
    

    You’re only going to affect code that relies on prototype.constructor to dynamically instantiate objects from instances.

    When you call new X, the JS engine doesn’t reference X.prototype.constructor, it uses the X as the constructor function and X.prototype as the newly created object’s prototype., ignoring X.prototype.constructor.

    A good way to explain this is to implement the new operator ourselves. ( Crockford will be happy, no more new 😉

    // `new` emulator
    // 
    // Doesn't reference `.constructor` to show that prototype.constructor is not used
    // when istantiating objects a la `new`
    function make(ctorFun, argsArray) {
      // New instance attached to the prototype but the constructor
      // hasn't been called on it.
      const newInstance = Object.create(ctorFun.prototype);
      ctorFun.apply(newInstance, argsArray);
      return newInstance;
    }
    
    // If you create a utility function to create from instance, then it uses the
    // inherited `constructor` property and your change would affect that.
    function makeFromInstance(instance, argsArray) {
      return make(instance.constructor, argsArray);
    }
    
    function X(jumps) {
      this.jumps = jumps;
    }
    
    // Flip the constructor, see what it affects
    X.prototype.constructor = function(jumps) {
      this.jumps = !jumps;
    }
    
    const xFromConstructorIsGood = make(X, [true]);
    const xFromInstanceIsBad = makeFromInstance(xFromConstructorIsGood, [true]);
    
    console.log({
      xFromConstructorIsGood,
      xFromInstanceIsBad
    });

    Inheritance in JS

    Libraries that help with JS inheritance implement inheritance and do rely on prototype.constructor with something in the spirit the following:

    function extend(base, sub) {
    
      function surrogateCtor() {}
      // Copy the prototype from the base to setup inheritance
      surrogateCtor.prototype = base.prototype;
      sub.prototype = new surrogateCtor();
      // The constructor property is set to the base constructor
      // with the above trick, let's fix it
      sub.prototype.constructor = sub;
    }
    

    You can see that in the above code, we have to fix the constructor property because it’s sometimes used to create instantiate an object when you only have an instance. but it doesn’t affect the actual constructor. See my post about JS inheritance http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html

    How to redefine a constructor
    If you really want to redefine a constructor, just do

    // If Rabbit had any custom properties on it 
    // (or static properties as some call it), they would not be copied, you'd have to do that manually using getOwnPropertyNames
    
    // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
    var oldProto = Rabbit.prototype;
    Rabbit = function() {...};
    Rabbit.prototype = oldProto;
    // Don't keep the old constructor
    Rabbit.prototype.constructor = Rabbit;
    

    Note that this would not affect code that had already copied that reference, for example:

    const myRefRabbit = Rabbit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have some openGL code (such code for example) /* FUNCTION: YCamera ::
I have such example. function Bar() { this.barVal = BarValue; } function Foo() {
Say I have this for example: $(function() { var seconds = 142.097019375; seconds =
For example, I have such a code: <?php $A = array( 'echoSmth' => function(){
I have such JavaScript code: <script> function Foo() { alert('Foo'); } $.post('file.php', {}, function(data)
For example I have such query: Query q = sess.createQuery(from Cat cat); List cats
I have such code in my JSF template: <h:form> <table id=users cellspacing=0> <a4j:repeat var=person
I have such template functions: template<class R> list<R> f(const boost::function<R()>&); template<class R, class A0>
I have another one question about functions reference. For example, I have such definition:
function example(){ var quantity = http://www.example.com/index.php?brandid=b%123&quantity=20; quantity = quantity.replace(-what regular expression should i user-,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.