Home > Article > Web Front-end > What is the prototype chain in es6
Prototype chain, simply understood is a chain composed of prototypes. When accessing an attribute of an object, it will first search on the attribute of the object itself. If it is not found, it will search on its __proto__ implicit prototype, that is, the prototype of its constructor. If it has not been found yet, It will then search in the __proto__ of the prototype of the constructor. In this way, searching upward layer by layer will form a chain structure, which is called a prototype chain.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JS, each function will have a prototype attribute when it is created. This attribute is a pointer pointing to an object, and this object is the prototype object of the function. (That is, prototype), it is a place used to share the properties and methods of all instances, so
The prototype is actually the companion of the function
(innate)
var a = [1, 2, 3, 4, 5]; a.__proto__ === Array.prototype; // true
Prototype chain, simply understood is a chain composed of prototypes. When accessing an attribute of an object, it will first search on the attribute of the object itself. If it is not found, it will search on its __proto__ implicit prototype, that is, the prototype of its constructor. If it has not been found yet, It will then search in the __proto__ of the prototype of the constructor, so that searching upward layer by layer will form a chain structure, which we call
prototype chain
.
Create a constructor:
function Person (name) { this.name = name } var cheng = new Person('Cheng'); var lin = new Person('Lin');
Print cheng and lin, you can see that there is a __proto__ attribute in both objects
Then I discovered
console.log(cheng.__proto__ === lin.__proto__); // true
So what exactly is this __proto__ attribute of the instance object?
console.log(cheng.__proto__ === Person.prototype); // true console.log(lin.__proto__ === Person.prototype); // true
Summary: The instance object has a non-enumerable attributeproto. This attribute is a pointer pointing to the prototype of its constructor, which is the prototype object. The instance can pass proto Access methods on the prototype of the constructor
Simply put, The __proto__ of the instance object points to the prototype of the constructor
So, what is the prototype of this constructor?
Printing Person.prototype, you can see that there is also a __proto__ attribute
console.log(Person.prototype.__proto__ === Object.prototype)
Summary: The prototype of the function is essentially an ordinary object, so he is an instance from Object, therefore, the proto property of the prototype object points to Object.prototype.
Go down and find where __proto__ of Object.prototype points to?
Finally found that it points to null
Summary: Any data in Javascript can eventually find Object.prototype## along its own prototype chain
The __proto__ of the instance object points to the prototype of the constructor, the __proto__ of the constructor points to the prototype of Object, and the __proto__ of Object ultimately points to null
javascript advanced tutorial]
The above is the detailed content of What is the prototype chain in es6. For more information, please follow other related articles on the PHP Chinese website!