Js prototype chain principles

Source: Internet
Author: User

At the beginning, the inventor of ECMAscript designed the linked list to simplify the language and maintain the inherited attributes ..
The linked list is not learned in the data structure. A position in the linked list is equivalent to a pointer and points to the next struct.

The same is true for _ proto _. When you define a prototype, _ proto _ of the Instance points to a struct, the directed struct is called the prototype of the instance.

The text is a bit difficult to say.
Copy codeThe Code is as follows:
Var foo = {
X: 10,
Y: 20
};


When I do not specify _ proto _, foo reserves such an attribute,

If there is a clear point, the linked list will be linked.

Obviously, B and c share the attributes and methods of a and have their own private attributes.

_ Proto _ is also pointed to by default. It points to the highest level of object. prototype, while the _ proto _ of object. prototype is null.
Copy codeThe Code is as follows:
Var a = {
X: 10,
Calculate: function (z ){
Return this. x + this. y + z
}
};
Var B = {
Y: 20,
_ Proto __:
};

Var c = {
Y: 30,
_ Proto __:
};

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


Understand the essence of the property link pointer _ proto .. Then we can understand constructor.

When a prototype is defined, a prototype object is constructed. The prototype object is stored in the prototype method used to construct the prototype function.
Copy codeThe Code is 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]

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.