JS Advanced --- prototype and prototype chain

Prototype and prototype chain

Prototype chain is a relationship, the relationship between the instance object and the prototype object, by way of example the relationship between a subject using a prototype browser (the __proto__) to contact the

 

  1. Custom constructor, by instantiating, create an instance
  2. Examples of objects __proto__ is the prototype, the browser used
  3. Constructor prototype is a prototype, programmers

 

 

 

 

    // use the object ----> use of object attributes and object, the object must first use constructors 
    // Constructor 
    function the Person (name, Age) {
       // attributes 
      the this .name = name;
       the this = .age Age;
       // in the constructor method of 
      the this .eat = function () { 
        the console.log ( "delicious food" ); 
      }; 
    } 
    // add shared attributes 
    Person.prototype.sex = "M " ;
     // add a shared approach 
    Person.prototype.sayHi = function () { 
      console.log ( " You Well, how handsome is that handsome " ); 
    }; 
    //Instantiating the object, and initializes 
    var per = new new the Person ( "Bob", 20 is ); 
    per.sayHi (); 
    // if you want to use the properties and methods, and the value of each attribute in the object are the same, the method of operation in each object are also the same, then, in order to share data, save memory space, the properties and methods that can be assigned by means of the prototype 

    console.dir (per); // structure of instance objects 
    console. the dir (the Person); // structure constructors 

    // prototypes prototype prototype point __proto__ constructor and the object is the same instance 

    // __proto__ prototype instance object points is the prototype prototype constructor 
    console .log (per .__ proto __ == Person.prototype);
     // instance __proto__ prototype object, the browser uses 
    // constructor prototype is a prototype, programmers 

    @ prototype chain: a relationship, the relationship between the object and the prototype object instance, by the relationship between the prototype (the __proto__) to contact the

Guess you like

Origin www.cnblogs.com/jane-panyiyun/p/12151715.html