Front-end basis - often test content

First, what is the prototype, the prototype chain is what? And a prototype object constructorattributes

 

"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 prototypeproperty, 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.prototypethat Objectconstructors prototypeproperties. That is, all objects inherit the Object.prototypeproperties. That's all objects have valueOfand toStringmethodological reasons, because it is from Object.prototypeinherited.

So, Object.prototypethe object does not have its prototype it? The answer is Object.prototypethe prototype null. nullDoes not have any properties and methods, do not own prototype. Therefore, the prototype chain is at the end null.

3. prototype object constructorattributes

prototypeObject has a constructorproperty, pointing to default prototypeconstructor object is located. constructorRole attribute is an instance of an object can be known, which in the end is generated constructor.

On the other hand, with the constructorproperties, you can create a new instance of the object from one instance to another.

constructorProperty indicates the relationship between the prototype object constructor, if you modify the prototype object, usually also modify constructorproperties that prevent the wrong time reference.

Modifying the prototype object, generally also modify the constructorpoint 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

Different 10.cookie, session, localStorage, sessionStorage of

Guess you like

Origin www.cnblogs.com/songsongblue/p/11601091.html