JS prototype chain

Source: Internet
Author: User

Prototype chain

When you create a function, you automatically add a prototype property to the function,

This property points to the prototype object, which, by default, only blocks one constructor property in the object,

It is a pointer to the function where the prototype property is located.

var person = function () {};

var p = new person ();

Let's see what this new has done. We can split the new process into the following three steps:

<1> var p = {}; Create an empty object p<2> p.__proto__ = Person.prototype; Modify P's __proto__ attribute to point to the person's prototype<3> Person.call (p); Modifies the this pointer of the person object, pointing to the object P
Here's an example to understand the so-called prototype chain

var person = function (name) {    this.name = name;} Person.prototype.say = function () {    alert (this.name);} var p = new Person (' Jack ');p. Say ();


You can see the __proto__ pointer of the object P pointing to the Person.prototype,the __proto__ of Person.prototype also pointed to Object.prototype, andthe Object.prototype __proto__ is null and the prototype chain ends.


The prototype chain is maintained through __proto__

So what is __proto__?

Each object initializes a property within it, which is __proto__, When we access the properties of an object, if this property does not exist inside this object (local property), then it will go to __proto__ for this attribute. And this __proto__ is also an object, and will have their own __proto__, so it has been constructed, until the last object of the __proto__ property is null, the formation of what we normally call the prototype chain.

Lookup Order

Each object has a prototype object, because the prototype object itself is also an object, according to the definition above, it also has its own prototype, and its own prototype object can have its own prototype, so that the formation of a chain, this is the prototype chain. when the JAVASCRITP engine accesses an object's properties, if it is not found in the object itself, it goes to the prototype chain and, if found, returns the value directly, if the entire chain is traversed and no property is found, The undefined is returned. The prototype chain is typically implemented as a list, so that you can find it in a certain order

Tip

If the prototype chain, local properties have the same method, then it will take precedence to find and execute locally, and the method on the prototype chain will not execute. Similarly, the search for attributes is in this order.

The following two small examples illustrate the property and method lookup order:

1>

function Foo () {    This.say = function () {        alert (' local method ');}    } A.prototype.say = function () {    alert (' prototype method ');} New Foo (). Say ();
Obviously, the result of alert is definitely the "local method" message, which shows that the search for properties and methods starts locally.

2>

var base = {      Name: "Base",      getinfo:function () {         return this.id + "," + THIS.name;      }  }      var ext = {      id:0,      __proto__: Base  }      Console.log (Ext.getinfo ());//0,base//var base = {      Name: "Bas E ",      getinfo:function () {         return this.id +", "+ THIS.name;      }  }      var ext = {      id:0,      name: "ext",    __proto__: Base  }      Console.log (Ext.getinfo ());//0,ext
The former result is "0, base", ext did not find the name attribute, the prototype chain through the __proto__ to find the base body, just in base has this attribute, the result is "base".

The latter result is "0,ext", which is because the attribute name was first found in ext and the result is "ext". It is important to note that when Ext calls the GetInfo in base, this point is actually the Ext object, not the base, so the result will be "ext".


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JS 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.