Prototype and prototype chain
1. Explicit prototyping
prototype
Is a pointer to an empty object (constructor
This object is a stereotype object and is called an explicit stereotype- each
function
There is aprototype
Property, except for the arrow function - When a function is instantiated, if the instantiated object calls a method or property that the instantiated object doesn’t have, the constructor’s prototype object will be found
- Constructor only
prototype
It makes sense. The other functionsprototype
No use
2. Implicit prototyping
- Each Object has an implicit stereotype
__proto__
- The implicit stereotype of an object points to the display stereotype of its constructor
- When an object obtains a property that it does not own, it follows an implicit stereotype
__proto__
Find an explicit prototype for its constructorprototype
- When you set a property or method to an object, you don’t set it directly to the object through an implicit stereotype
3.constructor
- use
constructor
You know by whom the object is instantiated (get the constructor of the object) constructor
Exists on the constructor’s prototype object- Any object is instantiated, so the constructor’s prototype object can be found, so every object can be called
constructor
attribute - constructional
prototype
Refers to its prototype object, prototype objectconstructor
Points to its constructor.
4. The prototype chain
Action: The rule for an object to find a property, the end of the prototype chain isnullSteps: (1) first look at their own properties (2) If you can't find it, follow the implicit stereotype attribute layer by layer (3) The process ends when it is found (4) Until I find it`Object.prototype.__proto__`If it has not been found, the return value isundefined
Copy the code
5.
- Can implement inheritance (get other people's properties and methods), can use reuse - use: constructor or composite inheritance or ES6classGrammar - Specific:ReactClass componentsCopy the code