What is the use of prototype chains in js?

take a look at the prototype and just add an attribute to the object. If the object needs this property, can"t you just add it at the beginning of creation? If you need this property for an instance, you might as well refine the subclass and add the attribute (es6) to the class. Do you have a partner to talk about the scenarios in which must be used in actual development?


as you said, who uses to add to whom, if I have 100 functions using the same method, should I redefine them in these 100 functions or write a public class that can be called when everyone uses it.

the way js wants to call a public class is to inherit the prototype.

for example, the native [] .forEach () method can be used in any array because forEach () is defined on the constructor function Array () {} of the array, that is, on the prototype of the array;


for example, axios


prototype chain is used in vue. The concept of a class is simulated by a prototype chain. If there is no prototype chain, there is no parent class.
you often use methods such as toString in your actual development. There is no prototype chain, so do you define them all?
when accessing an object A.B, it will first look for the b attribute of the current object. If not, then follow prototype to find it until it finds Object.prototype . If not, it returns undefined , so it is everywhere.

Menu