js prototype and prototype chain (common, easy to understand)

First of all, we need to understand what a prototype is. For each function we create, the parser will add a property prototype to the function. This property corresponds to an object, which is what we call a prototype object. The prototype object also has an attribute constructor to point to the function object. When the function is used as an ordinary function, the prototype has no effect. When a function is called as a constructor, the object it creates will have an implicit property pointing to the prototype object of the constructor, which we can access through __proto__. Each function function has a prototype, that is, an explicit prototype. Each instance object has a __proto__, which is the implicit prototype, and the implicit prototype of the object corresponds to the value of the explicit prototype of its constructor.
insert image description here

So what is the prototype chain? When reading the property value of an object, it will first search in its own property. If it does not automatically search its own prototype chain, if it has not continued to search upwards along the __proto__ chain, it will return when it is found. , not found, returns undefined. Generally, the prototype object of Object is the end of the prototype chain. But note that when setting the object property value, the prototype chain will not be searched. If the current object does not have this property, directly add this property and set its value. Methods are generally defined in the prototype chain, and properties generally define the object itself through the constructor. .insert image description here

Guess you like

Origin blog.csdn.net/m0_59722204/article/details/128894440