What is the prototype chain

Prototype chain:

Everything in JavaScript is an object, and there is also a relationship between objects and objects, and they do not exist in isolation. The inheritance relationship between objects, in JavaScript, points to the parent class object through the prototype object until it points to the Object object, thus forming a chain pointed to by the prototype, which is called the prototype chain in technical terms.

 

 

 

use:

function Fn() {}// Fn is the constructor
var f1 = new Fn();//f1 is the object created by the Fn constructor
The value of the prototype attribute of the constructor is the object prototype. (Fn.prototype is the prototype of the object)
The type of the prototype attribute value of the constructor is the object typeof Fn.prototype===object.
The constructor attribute in the object prototype points to the constructor (Fn.prototype.constructor===Fn)
object The __proto__ attribute value is the prototype of the object. (f1.__proto__ is the object prototype)
Fn.prototype===f1.__proto__ In fact, the two of them are the same object---the prototype of the object.
All Fn.prototype.__proto__===Object.prototype
typeof Object.prototype ===object.
Object.prototype.__proto__===null.

When I discuss prototypes, I mean the relationship between objects and prototype objects. So the prototype chain is also called the object chain.

 

 

 

 

Summary: The prototype object constructor of a function points to the function itself by default. In addition to the prototype attribute, the prototype object also has a prototype chain pointer __proto__ in order to achieve inheritance. This pointer points to the prototype object of the previous layer, and the prototype object of the previous layer The structure is still similar, so __proto__ is always used to point to the prototype object of Object, and the prototype object of Object uses Object. Explains why all javascript objects have the basic methods of Object.

 

Guess you like

Origin blog.csdn.net/yjnain3066/article/details/127723233