Javascript Prototype Constructor Confusion
function Test() { this.name = 'test name'; } console.log(Test.prototype.constructor.prototype.constructor); I can't understand why is this a infinite chain of constructor -
Solution 1:
Well, every Function Object, by default has a .prototype
property, which references the prototype object for this function (becomes only important if used as constructor).
And every prototype object
by default has a reference to the constructor
function, which of course, points back, to the constructor function (in your case Test()
).
So, here we go
Test.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor
Solution 2:
This is a desired behavior.
The constructor
property of a prototype refers to the object that owns the prototype (so it refers back to itself).
Post a Comment for "Javascript Prototype Constructor Confusion"