Each function we create has a prototype property, which is an object, and its purpose is to contain properties and methods that can be shared by all instances of a specific type. (There is an attribute under this object. This attribute is the application of another object. This attribute is an object.)
function is an object, and the prototype of function is also an object, and they all have common characteristics of objects. That is: the object has the attribute __proto__, and each object will initialize a property inside it, that is __proto__. When we access the attribute of an object, if this attribute does not exist within the object, then it will look for this attribute in __proto__. This __proto__ will have its own __proto__, so we keep searching, which is what we usually call the concept of prototype chain. __proto__ can be called an implicit prototype. An object's implicit prototype points to the prototype of the constructor that constructs the object, which also ensures that the instance can access properties and methods defined in the constructor prototype.
Function, in addition to having the above-mentioned _proto_ attribute like other objects, also has its own unique attribute - prototype attribute (prototype). This attribute is a pointer, pointing to an object. The purpose of this object is to contain the properties and methods shared by all instances (we call this object a prototype object). prototype is the prototype object of that object instance created by calling the constructor. The advantage of using a prototype object is that all object instances can share the properties and methods it contains. Instead of defining the information of the object instance in the constructor, this information can be added directly to the prototype object. The prototype object also has a property called a constructor. This property contains a pointer that points back to the original constructor.
prototype and __proto__ contact
Both prototype and __proto__ point to prototype objects. Any function (including constructors) has a prototype attribute, pointing to the prototype object of the function. Similarly, any object instantiated by the constructor has a __proto__ attribute (__proto__ is not a standard attribute. ECMA-262 edition 5 calls this attribute or pointer [[Prototype]], which can be accessed through the () standard method), pointing to the prototype object of the constructor. ---sinのstone
Difference between ptototype and __proto__
//The prototype attribute when a is a constructor is not equal to the __proto__ attribute when a is a normal function( == a.__proto__);//false (a.__proto__); //function (){} (a.__proto__ == );//true //A When a function is called as a normal function, its constructor is a built-in object function, so the prototype object it points to is.//In fact, this is the same as (b.__proto__ == ) //a as a constructor, its prototype, and its prototype(); //a{} (.__proto__); //Object{} //A When a function is a normal function, its prototype(a.__proto__.__proto__); //Object{} (a.__proto__.__proto__ == .__proto__); //true
All objects have the __proto__ attribute. In addition to the __proto__ attribute, this special object of the function also has the unique prototype attribute prototype. The prototype object has two properties by default, the constructor property and the __proto__ property. The prototype attribute can add shared (inherited) methods and attributes to functions and objects, and __proto__ is the prototype chain method for finding a certain function or object. constructor, this property contains a pointer, pointing back to the original constructor.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.