The prototype

Every JS object has a proto property, which points to the prototype. This property is not recommended today. It was implemented by browsers in the early days to give us access to the internal property [[prototype]].

Here’s what Proto has to offer:

The prototype
object
obj
__proto__
A prototype object
A prototype object
constructor
The constructor

__proto__. Constructor === Object

The stereotype’s constructor property points to the constructor

obj.__proto__.constructor
prototype
obj.__proto__

Conclusion:

  • The object’s__proto__The property points toThe prototype;
  • The prototypetheconstructorThe property points to againThe constructor;
  • The constructorAgain through itprototypeProperty pointing backThe prototype;

::: warning Not all functions have constructor attributes, such as function.prototype.bind () :::

Prototype chain

Let’s take a look at the relationship between prototypes and prototype chains and figure it out once and for all.

After looking at this picture, let’s explain what a prototype chain is. In essence, a prototype chain is a chain of objects linked together by a __proto__ attribute. The reason why an obj object can access a valueOf function is because OBj found valueOf through the prototype chain.

Look at the picture and sort it out:

  • __proto__ : This attribute is given to objects (emphasis on objects); A function is also an object, so a function has this property, which points to the constructor’s prototype object;

  • Prototype: Functions have this property. Normal objects don’t have this property. It is the prototype object for the constructor;

  • Constructor: This is a property on the prototype object that points to the constructor.

Conclusion:

  • Every object has it__proto__Properties,__proto__= = >Object.prototype(Object constructor prototype Object);
  • Each of these functions__proto__andprototypeProperties;
  • eachA prototype objectThere areconstructor__proto__Attribute, whereconstructorRefers back to ‘constructor ‘, and__proto__Point to theObject.prototype;
  • objectIs the ancestor of objects, all objects can pass through__proto__Property to find it;
  • FunctionIs the ancestor of all functions, all functions can pass__proto__Property to find it;
  • Each function has oneprototypeBecause ofprototypeIs an object that points to the prototype object of the constructor
  • The object’s__proto__Attribute points toThe prototype.__proto__A stereotype chain is formed by linking objects and stereotypes

reference

  • Inheritance and prototype chains
  • JavaScript goes from prototype to prototype chain