The constructor property always points to the constructor that creates the current object. For example, the following example:
The code is as follows |
Copy Code |
Equivalent to var foo = new Array (1, 56, 34, 12); var arr = [1, 56, 34, 12]; Console.log (Arr.constructor = = Array); True Equivalent to var foo = new Function (); var Foo = function () {}; Console.log (Foo.constructor = = Function); True Instantiating an Obj object by a constructor var obj = new Foo (); Console.log (Obj.constructor = = = Foo); True
By combining the above two pieces of code, you get the following conclusion Console.log (Obj.constructor.constructor = = Function); True
|
But when constructor met prototype, something interesting happened.
We know that each function has a default attribute prototype, and this prototype constructor the default point to this function. As shown in the following example:
The code is as follows |
Copy Code |
function person (name) { THIS.name = name; }; Person.prototype.getName = function () { return this.name; }; var p = new Person ("Zhangsan");
Console.log (P.constructor = = person); True Console.log (Person.prototype.constructor = = person); True Merging the last two lines of code results in the following Console.log (P.constructor.prototype.constructor = = person); True
|
When we redefined the prototype of a function (note: The difference from the previous example, which is not a modification but a overwrite), the behavior of the constructor property is a bit strange, as the following example:
The code is as follows |
Copy Code |
function person (name) { THIS.name = name; }; Person.prototype = { Getname:function () { return this.name; } }; var p = new Person ("Zhangsan"); Console.log (P.constructor = = person); False Console.log (Person.prototype.constructor = = person); False Console.log (P.constructor.prototype.constructor = = person); False
|
Why, then?
It turns out that overwriting person.prototype is equivalent to doing the following code:
The code is as follows |
Copy Code |
Person.prototype = new Object ({ Getname:function () { return this.name; } });
|
And the constructor property always points to the constructor that created itself, so Person.prototype.constructor = = Object at this point, that is:
The code is as follows |
Copy Code |
function person (name) { THIS.name = name; }; Person.prototype = { Getname:function () { return this.name; } }; var p = new Person ("Zhangsan"); Console.log (P.constructor = = = Object); True Console.log (Person.prototype.constructor = = = Object); True Console.log (P.constructor.prototype.constructor = = = Object); True
|
How to fix this problem? The method is also very simple, the Person.prototype.constructor can be overwritten again:
code is as follows |
copy code |
function Person (name) { this.name = name; }; Person.prototype = new Object ({ getname:function () { return this.name; } }); Person.prototype.constructor = person; var p = new Person ("Zhangsan"); Console.log (P.constructor = = person);//True Console.log (Person.prototype.constructor = = person);//True Console.log (P.constructor.prototype.constructor = = person); True |