This time, thoroughly understand the prototype and prototype chain

Why Learn Prototypes and Prototype Chains?

Prototype and prototype chain are the focus of the interview and the top priority of JS foundation. Therefore, this is a fortress that we must overcome on the way forward. Let us systematically sort out the prototype and prototype chain together~

Three Key Concepts of Prototype and Prototype Chain

The three key concepts of prototype and prototype chain are: __proto__, prototype, constructor.

1. __proto__ (implicit prototype)

First of all, we need to clarify who is proto pointing to? It points to the prototype object of the parent class constructor. proto is usually called an implicit prototype, and prorotype is an explicit prototype. We can say 一个对象的隐式原型指向了该对象的构造函数的显式原型。that the properties and methods defined on the explicit prototype are passed to the constructor through the implicit prototype. , so that the instance can easily access the methods and properties on the constructor prototype.

There is a special problem that needs attention in the knowledge of proto:

RQ1: What is the essence of the prototype object prototype? Its essence is an object, then its implicit prototype refers to the prototype object of its constructor, which is Object.prototype.

RQ2: What is the implicit prototype of a constructor?

The essence of a constructor is a function, so the displayed prototype of its constructor is Function.prototype.

In order to better solve the above two problems, the final thing is to understand the following picture, which contains many error-prone points:

image.png

2. prototype (explicit prototype)

Prototype refers to an explicit prototype, which points from a function to an object. The purpose of prototype design is to achieve inheritance, so that all forces created by a specific function share properties and methods, or it can be said that a certain constructor is instantiated. All objects can find public methods and properties.

3. constructor

The function pointed to by the constructor is the constructor of the object. The constructor property of the prototype object of the instance points to its corresponding constructor, and the constructor of a function points to the Function root constructor, and the constructor of the Function function points to the itself.

Remember the following image:

image.png

high frequency classical problem

RQ: What is the end of the prototype chain?

The end of the prototype chain is actually null, as we can see from the section on implicit prototypes.

RQ: How to understand prototype and prototype chain?

The focus on prototype and prototype chain is systematically introduced from the above three attributes, and each attribute is explained clearly, and the relationship between prototype and prototype chain is also made clear.

Guess you like

Origin blog.csdn.net/sinat_41696687/article/details/123145167