Why use object.prototype.constructor in OOP javascript?
Posted
by
Matt
on Stack Overflow
See other posts from Stack Overflow
or by Matt
Published on 2012-01-31T04:50:33Z
Indexed on
2013/10/27
9:55 UTC
Read the original article
Hit count: 230
JavaScript
|oop
I've recently started reading up on OOP javascript and one thing that authors seem to skip over is when an object A has been declared and suddenly I see "A.prototype.constructor =A; For example,
var A = function(){}; // This is the constructor of "A"
A.prototype.constructor = A;
A.prototype.value = 1;
A.prototype.test = function() { alert(this.value); }
var a = new A(); // create an instance of A
alert(a.value); // => 1
So I run the command in firebug "var A = function(){};"
and then "A.Constructor" Which reveals it's a function. I understand this.
I run the code "A.prototype.constructor = A;" and I thought this changes the A constructor from Function to A.
The constructor property of A has been changed right? Instead when I run "A.constructor" it gives me function () still.
What's the point?
I also see A.constructor.prototype.constructor.prototype.. what is going on?
© Stack Overflow or respective owner