The JS prototype chain principle looks at the diagram to explain _javascript skill

Source: Internet
Author: User
In order to simplify the language and retain the inherited attributes, ECMAScript's inventor designed the list.
In the data structure of the linked list is not, the list has a position equivalent to the pointer, pointing to the next structure.

So __proto__ also, whenever you define a prototype, the equivalent of the __proto__ of the instance to a struct, then the point to the struct is called the prototype of the instance.

The text is a bit of a circle, look at the picture.
Copy Code code as follows:

var foo = {
X:10,
Y:20
};


When I don't specify __proto__, Foo also reserves one of these attributes,

If there is a clear point, then the linked list is linked.

It is obvious that B and C share A's properties and methods in the image below, while having their own private properties.

__proto__ The default also has a point. It points to the highest object.prototype, while the Object.prototype __proto__ is empty.
Copy Code code as follows:

var a = {
X:10,
Calculate:function (z) {
Return this.x + This.y + Z
}
};
var B = {
Y:20,
__PROTO__: A
};

var C = {
Y:30,
__PROTO__: A
};

Call the inherited method
B.calculate (30); 60


Understand the nature of __proto__ this attribute link pointer. again to understand constructor.

When a prototype is defined, a prototype object is constructed, which is stored in the prototype method of constructing this prototype function.
Copy Code code as follows:

function Foo (y) {
This.y = y;
}

foo.prototype.x = 10;

Foo.prototype.calculate = function (z) {
return this.x+this.y+z;
};

var B = new Foo (20);

Alert (B.calculate (30));

"Reference Documentation"

Http://dmitrysoshnikov.com/ecmascript/javascript-the-core/

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.