First, what is the prototype, the prototype chain is what? And a prototype object constructor
attributes
"JavaScript Tutorial - Road Network"
https://wangdoc.com/javascript/oop/prototype.html
1. Prototype
All the properties and methods of the prototype object can be instantiated object sharing. That is, if the properties and methods defined on the prototype, then all instances of objects can share not only saves memory, but also reflects the link between the instance of an object.
JavaScript provides that each function has a prototype
property, point to an object.
For general functions, the basic properties of the useless. However, for the constructor, the generated instances when the attribute is automatically become the prototype of the object instance.
Properties of the prototype object instance is not a property of the object itself. As long as the modified prototype object, change will be immediately reflected in all instances of objects.
As an example, when the object itself is not a property or method, it will go to the prototype object to find this property or method. This is so special prototype object.
If there is an instance of an object property or method itself, it will not go looking for this prototype object property or method.
In summary, the role of the prototype object, is to define the properties and methods shared by all instances of objects . This is the reason it is called the prototype object, and the object instance can be viewed as child objects derived from the prototype object.
2. prototype chain
JavaScript provides that all objects have their own prototype object (prototype). On the one hand, any object can act as prototype of other objects ; on the other hand, since the prototype objects are objects, so it also has its own prototype . Thus, it will form a "prototype chain" (prototype chain): subject to the prototype, the prototype of the prototype and then ......
If the layers of traced, the prototype of all objects can ultimately be traced back to Object.prototype
that Object
constructors prototype
properties. That is, all objects inherit the Object.prototype
properties. That's all objects have valueOf
and toString
methodological reasons, because it is from Object.prototype
inherited.
So, Object.prototype
the object does not have its prototype it? The answer is Object.prototype
the prototype null
. null
Does not have any properties and methods, do not own prototype. Therefore, the prototype chain is at the end null
.
3. prototype object constructor
attributes
prototype
Object has a constructor
property, pointing to default prototype
constructor object is located. constructor
Role attribute is an instance of an object can be known, which in the end is generated constructor.
On the other hand, with the constructor
properties, you can create a new instance of the object from one instance to another.
constructor
Property indicates the relationship between the prototype object constructor, if you modify the prototype object, usually also modify constructor
properties that prevent the wrong time reference.
Modifying the prototype object, generally also modify the constructor
point properties.
Second, inheritance
Inherited constructor
Let a constructor inherits from another constructor, they are very common requirement. This can be achieved in two steps.
The first step is the constructor of the child class, call the constructor of the parent class.
The second step is to make a prototype of a subclass of parent class prototype point, so subclasses can inherit the parent class prototype.
CMB electric front end
Links: https://www.nowcoder.com/discuss/287857?type=all&order=time&pos=&page=0
1. Self-introduction
2. say under their own projects
3.vue two-way data binding
4. Copy shades
5. bubbling event delegation
6. Closure
7. Inheritance
8. prototype chain
9. Design Patterns