After the process of creating the object and the new method is clear, it is much easier to understand the concept of the prototype.
The purpose of the prototype exists in order to be more memory-efficient inheritance.
I think the main thing in the prototype is to figure out what these 4 concepts are, what the explicit prototypes point to, what the implicit prototypes point to, what the constructor points to, and what the prototype chain is.
A picture wins thousands of words. The following diagram explains all of these concepts.
Here's a summary of some of the basic features of prototypes for deeper understanding:
1. In JS, everything is the object. A method (Function) is an object, and the prototype (Function.prototype) of a method is an object. Therefore, they all have characteristics that are common to objects.
That is, an object has an attribute __proto__, which can be called an implicit prototype, and an implicit prototype of an object points to the prototype of the constructor that constructs the object, which ensures that the instance has access to the properties and methods defined in the constructor prototype. ES5 has a Get method object.getprototypeof () for this built-in property standard.
2. Method (Function) This special object, in addition to the same as other objects have the above __proto__ attribute, but also has its own unique properties--prototype properties (prototype), this property is a pointer to an object, The purpose of this object is to include properties and methods shared by all instances (we call this object a prototype object). The prototype object also has a property called constructor, which contains a pointer to the original constructor.
3. All objects are created by a function. Note: {} is more efficient than new Object ().
4. The implicit prototype (__proto__) of the function object points to Function.prototype.
5. The implicit prototype (__PROTO__) of the explicit prototype (prototype) of the function object points to Object.prototype.
6. An implicit prototype (__proto__) of a non-function object points to the object to which the prototype property of its constructor points.
7. object.prototype.__proto__ points to null.
8. The implicit prototype (__proto__) of the function points to function.prototype, because function is also an object of functions.
9. Objects are reference types, so explicit prototypes (prototype) and implicit prototypes (__proto__) are also reference types, so the prototype modification of the parent (or parent, or higher) constructor directly affects its children (as well as the children and children, and lower) when the property is added.
Reference Link: https://www.zhihu.com/question/34183746/answer/58155878
JS prototype and prototype chain knowledge collation