Home >Web Front-end >JS Tutorial >Introduction to prototype chain prototype in JavaScript_javascript skills
Inheritance in JavaScript is accomplished through the prototype chain: each object has another object inside it as its prototype, and the object inherits properties from this prototype. For each object, you can access its prototype object in the following three ways:
1.__proto__. An object's prototype object can be accessed through its __proto__ attribute. This property is only supported in Firefox, Safari and Chrome, not IE and Opera.
2.Object.getPrototypeOf(). You can pass the object as a parameter to the Object.getPrototypeOf() method, and the prototype object of the object will be returned after execution. This method is only supported in the ECMAScript 5 standard.
3.o.constructor.prototype. Access the prototype object by first obtaining the constructor function of the object, and then accessing the prototype property of the constructor function. The prerequisite for using this method is that there is a constructor attribute pointing to the constructor in the object.
To determine whether there is a prototype chain relationship between two objects, you can use the isPrototype() method:
For all objects created with literals, their prototype objects are Object.prototype (as a special object, Object.prototype has no prototype object):
For all objects created with the new operator, their prototype objects are the prototype attributes of the constructor function:
The process of using the new operator to create objects in JavaScript is as follows:
1. Create a new empty object.
2. Point the __proto__ attribute of this object to the prototype attribute of the constructor function.
3. Use this object as this parameter to execute the constructor function.
From the above creation process, it can be concluded that all objects constructed from the same constructor function have their __proto__ attributes (that is, their prototype objects) equal, that is, there is only one prototype object: