Summary of JavaScript prototype chain learning

Source: Internet
Author: User

Both Function and Object are Function instances.

The parent prototype of a Function points to the prototype of a Function. The parent prototype of a Function is the prototype of an Object.
The Object parent prototype also points to the Function prototype.
An instance's portrait. Its default parent prototype is the display prototype of its constructor.
[Each object has a hidden object attribute that is used to point to its parent object (a function for constructing the object). This is called a parent object or an implicit object ). Because the prototype is also like a prototype, the prototype also has a parent prototype. The prototype of an Object is the top layer (prototype root) of all parent prototypes, thus forming the so-called prototype chain]

Image attribute access principles

When reading an attribute from an object, if the object does not exist in its own attribute list, it will search for the parent object associated with it, if the parent prototype does not have such a property in the property list, it searches for the parent prototype of the parent prototype until it is found or until the parent prototype [Object. prototype] The image attribute list has been searched.
The method of calling an object is the same as the process of accessing attribute search, because the function object of a method is an attribute value of an object.
Instance:
Copy codeThe Code is as follows:
Object. prototype. m1 = function (){
Alert ("I am a lion ");
}
Function Class1 (str ){
This. p1 = str;
}
Function Class2 (){}
Class2.prototype. m1 = function (){
Alert ("hello ");
}
Var n1 = new Class1 ("Mao Lions ");
// @__ Proto _ the property is a reference to a prototype like a parent.
// @ Object. prototype. _ proto __= null
/*
Prototype chain of n1
N1. _ proto __= Class1.prototype
Class1.prototype. _ proto __= Object. prototype

*/
Var n2 = new Class2 ();
/*
Prototype chain of n2
N2. _ proto __= Class2.prototype
Class2.prototype. _ proto __= Object. prototype
*/
N1.m1 (); // = Object. prototype. m1 ();
N2.m1 (); // === Class2.prototype. m1 ();
Alert (n1.p1); // Mao lions
Alert (n2.p1); // undefined

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.