How to understand prototype and prototype chain in 2023

1. What is the ulterior relationship between prototype and __proto__?

When it comes to prototype objects, there must be several concepts:
prototype, __proto__, constructor.

First explain what a prototype is, a prototype object. An object built into all functions by the V8 engine. That is, as long as a function is created (the class is also counted, because the essence of the class is also a function). Then this function will carry a prototype object prototype.

Let's talk about __proto__, whether it is a function or an object, it has the attribute __proto__. Now, let me bring you a very powerful universal formula to illustrate the relationship between __proto__ and prototype

No matter the __proto__ of a function or an object, it must be equivalent to the prototype object of the corresponding instantiated class.

201504141249489350_c_w_600.jpg

What do you mean, let me give you an example:

function People(name){
    
    
  this.name = name
}
let p1 = new People();

let arr = new Array();
let obj = new Object();

Now I have three pairs, p1 (instantiated through the People class), arr (instantiated through the Array class), obj (instantiated through the Object class).

Then I can say:

p1.__proto__ === People.prototype;
arr.__proto__ === Array.prototype;
obj.__proto__ === Object.prototype;

Then someone will say, you are not comprehensive. Not only objects have the __proto__ attribute; how to calculate the __proto__ of the function?

OK, for the People class above, if I write it in a different way! ! ! ! ! as follows:

let People = new Function('name','this.name = name')

Is it transparent at once, and it is perfect. It can be said that People is instantiated through the Function class, so! ! !

People.__proto__ === Function.prototype;
Array.__proto__ === Function.prototype;
Object.__proto__ === Function.prototype;

Besides instantiating objects and functions, is there anything else that has a __proto__ attribute?

Another special case is prototype. We know that the prototype object is brought out by V8 when the function is created, but who brings it out? The prototype object is the prototype object, the prototype object is still the object, and the object is instantiated from the Object, so:

People.prototype.__proto__ === Object.prototype

If you push it here, you will find that if Object.prototype also points to the prototype object of Object. Then the prototype chain is headless, so:

Object.prototype.__proto__ === null

Now, for the prototype chain and prototype object, is it very transparent. Go to the console and try it out, remember the most important formula above:

No matter the __proto__ of a function or an object, it must be equivalent to the prototype object of the corresponding instantiated class! ! !

2. What kind of aircraft is the constructor doing here?

For constructor, and prototype, the relationship between __proto__. It is not possible to find a clearer formula as above.

u=162216898,2553278571&fm=253&fmt=auto&app=138&f=JPEG.webp
Therefore, here we list the correspondence between the constructor of different things and the prototype or __proto__ of different things for different situations. Anyway, not much, just remember it briefly.

(1) The constructor of the object:

The constructor of an object is equivalent to, corresponding to the instantiated class itself:
[].constructor === Array

(2) The constructor of the function:

The constructor of a function is equivalent to, Function:
Array.constructor === Function

(3)prototype的constructor

The constructor of the prototype is equivalent to the corresponding function itself:
Array.prototype.constructor === Array

(4)__proto__的constructor

The constructor of __proto__ is equivalent to the corresponding instantiated class itself:
[].__proto__.constructor === Array

For the above formula, don't come to a mathematical equivalence relationship. For example, from the first and fourth articles, you come to a commutative law:
[] === [].__proto__
this is not an exchange, it can only show that the two share a constructor, and it does not mean that the two are the same thing.

Guess you like

Origin blog.csdn.net/weixin_46726346/article/details/131284088