R
rb
Hi,
I'm experimenting with javascript Objects' prototypes. There is one
behaviour which I don't understand when I update the prototype with
another object instance. I hope someone here can explain this
behaviour. Here's some code (inspired by Douglas Crockford's
presentation at Yahoo and available online):
// Define entities
/***************/
function Gizmo(id) { this.id= id; }
Gizmo.prototype.toString = function () { return ('gizmo ' +
this.id); }
function Hoozit(id) { this.id = id };
Hoozit.prototype.toString = function () { return ('hoozit ' +
this.id); }
// Instanciate an Hoozit
/********************/
my_hoozit = new Hoozit(1);
my_hoozit.toString(); //returns "hoozit 1" as expected
//Update Hoozit.prototype and get the expected result
/*************************************************/
Hoozit.prototype.toString = function () { return ('updated hoozit ' +
this.id); }
my_hoozit.toString(); //returns 'updated hoozit 1',
// Update Hoozit.prototype to another object's instance
//*************************************************/
Hoozit.prototype = new Gizmo();
my_hoozit.toString(); //returns "updated hoozit 1", although the
prototype has been changed.
// This doesn't call the toString from the newly set prototype.
//Update Hoozit.prototype with a new function
/*****************************************/
Hoozit.prototype.toString = function () { return ('again updated
hoozit ' + this.id); }
my_hoozit.toString(); //returns 'updated hoozit 1',
//This doesn't work anymore!
It seems as if the toString method has been moved to the instance when
assigning the new Gizmo to Hoozit.prototype. Is that correct? Why is
that?
Thanks
Raph
I'm experimenting with javascript Objects' prototypes. There is one
behaviour which I don't understand when I update the prototype with
another object instance. I hope someone here can explain this
behaviour. Here's some code (inspired by Douglas Crockford's
presentation at Yahoo and available online):
// Define entities
/***************/
function Gizmo(id) { this.id= id; }
Gizmo.prototype.toString = function () { return ('gizmo ' +
this.id); }
function Hoozit(id) { this.id = id };
Hoozit.prototype.toString = function () { return ('hoozit ' +
this.id); }
// Instanciate an Hoozit
/********************/
my_hoozit = new Hoozit(1);
my_hoozit.toString(); //returns "hoozit 1" as expected
//Update Hoozit.prototype and get the expected result
/*************************************************/
Hoozit.prototype.toString = function () { return ('updated hoozit ' +
this.id); }
my_hoozit.toString(); //returns 'updated hoozit 1',
// Update Hoozit.prototype to another object's instance
//*************************************************/
Hoozit.prototype = new Gizmo();
my_hoozit.toString(); //returns "updated hoozit 1", although the
prototype has been changed.
// This doesn't call the toString from the newly set prototype.
//Update Hoozit.prototype with a new function
/*****************************************/
Hoozit.prototype.toString = function () { return ('again updated
hoozit ' + this.id); }
my_hoozit.toString(); //returns 'updated hoozit 1',
//This doesn't work anymore!
It seems as if the toString method has been moved to the instance when
assigning the new Gizmo to Hoozit.prototype. Is that correct? Why is
that?
Thanks
Raph