Author: Doris links: www.zhihu.com/question/34… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.
It’s time to pull out this picture that I’ve been saving for years:
When I first learned javascript, I was also confused with the topic, so I summarized it well:
First, a few points should be made clear:
In JS, everything is an object.
A method is an object, and its prototype is an object. Therefore, they all have characteristics common to objects.
That is, an object has an attribute __proto__, which can be called an implicit stereotype. The implicit stereotype of an object points to the stereotype of the constructor that constructed the object. This also guarantees that instances can access the attributes and methods defined in the constructor stereotype.
The implementation of the prototype chain is Proto, not Prototype
2. The method (Function)
Method of this particular object, in addition to, and other objects with the same _proto_ attributes, and their own special attributes – the prototype (prototype), this attribute is a pointer, pointing to an object, the object’s purpose is to include all attributes and methods of the instance Shared (we call this object is a prototype object).
The prototype object also has a property called constructor, which contains a pointer to the original constructor.
Ok, with these two basic points in mind, let’s look at the picture above.
1. Constructor Foo()
The constructor’s prototype property Foo. Prototype refers to the prototype object, which has a common method that can be shared by all instances of constructor declarations (f1, f2 in this case).
2. Prototype object Foo. Prototype
Foo. Prototype holds the method shared by the instance and has a constructor pointer back to the constructor.
Example 3.
F1 and f2 are two instances of Foo, which also have the __proto__ attribute, pointing to the constructor’s prototype object, thus accessing all of the methods mentioned in 1 above.
In addition:
The constructorFoo()
In addition to being a method, it’s also an object__proto__
Property, to whom?
The prototype object that points to its constructor.
The Function constructor is Function, so __proto__ refers to function.prototype.
In addition to Foo(), Function() and Object() work the same way.
A prototype object is an object. It’s__proto__
Properties, who do they point to?
Similarly, the prototype object that points to its constructor. Here is the Object. The prototype.
Finally, object. prototype’s __proto__ property points to null.
Conclusion:
1. The object has the __proto__ attribute, which points to the prototype object of the object’s constructor.
2. In addition to the __proto__ attribute, the method has the prototype attribute, which refers to the prototype object of the method.
Finished, welcome all kinds of criticism is perfect discussion, common progress ~