JS Prototype and prototype chain

Source: Internet
Author: User

First, prototype and __proto__

1, prototype (explicit prototype): Each function will have a property named prototype after it is created, which points to the prototype object of the function. It exists only in functions, such as the following example:

 function   person (name) { this . Name = name;  this . showMe = function   () {alert ( this  .name);        }        };            Person.prototype.from  = function   () { Alert ( ' I come from prototype. ')        );  var  p = new  person (' JS ' ); P.from ();  

The person owns the prototype object, we add a from method to the object, the constructor has a prototype property, and the property is a pointer to the corresponding prototype object (note that a property is an object). Another important attribute here is that there is an attribute constructor in the prototype object that points to the corresponding constructor, which is a corresponding relationship.

P is a new instance of the person constructor, the new method is divided into 3 parts, the most important step is the 2nd step, for this example

<1> var p={}; In other words, initialize an object p.

<2> P.__proto__=person.prototype;

<3> Person.call (P); that is, construct p, which can also be called initialization p.

  p This object does not have a prototype property , but it has a __proto__ property, which is mentioned below.

Second, __proto__

Implicit prototype, each object has this property, the above mentioned new instance p, when instantiated, you can draw p.__proto__=person.prototype. So when we call P. Say (), first there is no Say this attribute in P, so, he needs to go to his __proto__ to find, that is, person.prototype, and we define the above person.prototype.say=function () {}; So, we found this method.

Specific examples are as follows:

  

1. Constructor Foo ()
The prototype property of the constructor Foo.prototype points to the prototype object, there are common methods in the prototype object, and all instances of the constructor declaration (this is F1,F2) can share this method.

2. Prototype Object Foo.prototype
Foo.prototype holds the method of instance sharing, with a pointer constructor the constructor.

3. Example
F1 and F2 are two instances of the Foo object, which also has a property __proto__, a prototype object that points to the constructor, which can be like all the methods of accessing the prototype object as described in 1 above.

Other than that:
The constructor foo () also has the __proto__ property,
The prototype object that points to its constructor. The function's constructor is function, so the __proto__ here points to function.prototype.

__PROTO__ properties of the prototype object
The prototype object that points to its constructor. This is object.prototype.

Finally, the __proto__ property of Object.prototype points to null. Therefore, the specific summary of these properties is: 1, __proto__: The prototype object that points to its constructor, all objects have this property 2, prototype: The prototype object to the function, only the function has 3, constructor: This property is literally understood, Is the constructor that points to it, and its meaning can be so understood. The constructor of the instance is the constructor of the Person,person is function and so on, the prototype chain and inheritance is actually the prototype chain based, through the __proto__ attribute of a layer of search, to find the parent object prototype method, get the use of the first look at the code:
function person (name) {this.name = Name;this.showme = function () {alert (this.name);}}; Person.prototype.from = function () {alert (' I come from prototype. ');}   function Subper () {}subper.prototype = new person (' JS ');   var son = new Subper (); Son.showme (); JS   son.from ();//i come from prototype.   

The person in the code has a prototype method, subper through the code of Subper.prototype = new person (' JS ') to implement the inheritance, new instance son can get the method of person.

  

The method of inheriting the implementation is: prototypeAn (instantiated) object set to the parent class; subper.prototype = new Person ();  Using the new method previously learned, it can be converted to subper.prototype.__proto__ = Person.prototype;  It can be seen from the figure that the __proto__ of the instance points to prototype, so p.__proto__.__proto__ = Person.prototype;   This creates a prototype chain that has been __proto__ as a connection.  In fact, only from the view, you can see __proto__ formed a line, subper instance--subper prototype (person instances)--person prototype, this is a prototype chain. Iv. the constructor in the process of succession to be continued

JS Prototype and prototype chain

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.