Prototype and prototype chain

1. Explicit prototyping

  • prototypeIs a pointer to an empty object (constructorThis object is a stereotype object and is called an explicit stereotype
  • eachfunctionThere is aprototypeProperty, 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 onlyprototypeIt makes sense. The other functionsprototypeNo 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

  • useconstructorYou know by whom the object is instantiated (get the constructor of the object)
  • constructorExists on the constructor’s prototype object
  • Any object is instantiated, so the constructor’s prototype object can be found, so every object can be calledconstructorattribute
  • constructionalprototypeRefers to its prototype object, prototype objectconstructorPoints 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