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 prototype
theconstructor
The property points to againThe constructor
;The constructor
Again through itprototype
Property 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__
andprototype
Properties; - each
A prototype object
There areconstructor
和__proto__
Attribute, whereconstructor
Refers back to ‘constructor ‘, and__proto__
Point to theObject.prototype
; object
Is the ancestor of objects, all objects can pass through__proto__
Property to find it;Function
Is the ancestor of all functions, all functions can pass__proto__
Property to find it;- Each function has one
prototype
Because ofprototype
Is 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