JS Advanced Syntax 04 Prototype Chain

1. The prototype attribute of the
function * Each function has a prototype attribute, which by default points to an Object empty object (called the prototype object)
* There is a property constructor in the prototype object, which points to the function object
2. Add attributes to the prototype object (Generally methods)
*Function: All instance objects of the function automatically have the properties (methods) in the prototype
//Code shortcut: Alt + shift + r (rename)
3. Explicit and implicit prototypes:
1. Each function has a prototype, that is, an explicit prototype (attribute)
2. Every instance object has a __proto__, which can be called an implicit prototype (attribute)
3. The value of the implicit prototype of an object is the value of its constructor The value of the explicit prototype
4. * The prototype property of the function: automatically added when the function is defined, the default value is an empty Object object.
*Object's __proto__ property: automatically added when the object is created, the default value is the prototype property value of the constructor.
*Programmers can directly manipulate explicit prototypes, but cannot directly manipulate implicit prototypes (before ES6)

      function Fn(){
    
    
        //内部语句:this.prototype = {} 
      }
      var fn = new Fn();    //内部语句:this.__proto__ = Fn.prototype ;
      console.log(Fn.prototype === fn.__proto__); //ture

4. Prototype chain
* When accessing the properties of an object, first search in its own properties, find and return, if not, then look up along the __proto__ chain, and return the property if found, and return undefined if it is not found in the end
*Alias: Implicit prototype chain; Function: Find the properties (methods) of the object
5. The __proto__ of all functions are the same, because they are all assigned values ​​from the prototype of the new Function() constructor.
6. Explicit functions The object pointed to by the type prototype is an empty Object instance object by default (when the explicit prototype of Object points to not Object)
javascript console.log(Fn.prototype instanceof Object ); //ture console.log(Object.prototype instanceof Object); //false console.log(Function.prototype instanceof Object); //true
7. All functions are instances of Function (including itself)
Function. proto === Function.prototype
8. The prototype object of Object is the end of the prototype chain
console.log(Object.prototype. proto ); //null
9. *When reading the property value of the object: it will automatically find it in the prototype chain.
*When setting the property value of the object: no The prototype chain will be searched. If there is no such attribute in the current object, the attribute will be added directly.
*The method is generally defined in the prototype, and the attribute is generally defined in the object itself through the constructor.

Guess you like

Origin blog.csdn.net/Vodka688/article/details/113444127