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