JavaScript inheritance based on the prototype chain

Source: Internet
Author: User

A JavaScript object is a dynamic property "package" (referring to its own property). A JavaScript object has a chain that points to a prototype object. When attempting to access the properties of an object, it searches not only on the object, but also on the prototype of the object, as well as the prototype of the object, and then layers up until it finds a property that matches the name or reaches the end of the prototype chain.

Following the ECMAScript standard, someObject.[[Prototype]] symbols are used to point to someObject the prototype. Starting with ECMAScript 6, [[Prototype]] access is available through Object.getPrototypeOf() and Object.setPrototypeOf() to accessors. This is equivalent to JavaScript's non-standard but many browser-implemented properties __proto__ .

However, it should not be func confused with the properties of the constructor prototype . The property that is directed to the instance object created by the constructor [[prototype]] func prototype . Object.prototypeproperty represents Object the prototype object.

Here's what happens when you try to access a property:

//Let's say we have an object o, which has its own properties A and B://{a:1, b:2}//O's [[[Prototype]] has properties B and C://{b:3, c:4}//Finally, O.[[prototype]]. [[[Prototype]] is null.//This is the end of the prototype chain, which is null,//by definition, NULL is not [[Prototype]].//in summary, the entire prototype chain is as follows://{a:1, b:2}---> {b:3, c:4}---> NullConsole.log (O.A);//1//is a is O's own property? Yes, the value of this property is 1Console.log (O.B);//2//B is O's own property? Yes, the value of this property is 2//There is also a ' B ' property on the prototype, but it will not be accessed. This is referred to as "attribute masking (property shadowing)"Console.log (O.C);//4//is c the self-attribute of O? No, let's see if there's a prototype.//c is O. property of [[Prototype]]? Yes, the value of this property is 4Console.log (O.D);//undefined//is d a self-attribute of O? No, let's see if there's a prototype.//D is O. property of [[Prototype]]? No, let's see if it's on the prototype .//O.[[prototype]]. [[[Prototype]] is null to stop the search//no d attribute, return undefined

 

Original link: Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

JavaScript inheritance based on the 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.