JavaScript classes and Inheritance: Constructor Property Quest

Source: Internet
Author: User
Tags constructor inheritance

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.