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?
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: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 referenceX.prototype.constructor
, it uses theX
as the constructor function andX.prototype
as the newly created object's prototype., ignoringX.prototype.constructor
.A good way to explain this is to implement the
new
operator ourselves. ( Crockford will be happy, no more new ;)Inheritance in JS
Libraries that help with JS inheritance implement inheritance and do rely on
prototype.constructor
with something in the spirit the following: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
Note that this would not affect code that had already copied that reference, for example:
This is wonderful workaround creating an object from a literal, not from constructor function.
Firstly, if you want
jumps
member to be contained in the object, rather than being just a local variable in the constructor then you needthis
keyword.And now you can easily now access
jumps
publicly the way you wanted:But still if you create another Rabbit object it will initially have 'yes' as defined in the constructor, right?
What you could do is creating a Rabbit from some default Rabbit Object. The concrete rabbits will always have the default value from default Rabbit object even when you change it on the fly unless you have changed the value in concrete rabbit object (implementation). This is a different than @Juan Mendes's solution which is probably the best but it can open another point of view.
Try the following