Analysis & prototype constructor

<Growth essays front end of a kindergarten class size, mistakes and shortcomings, it looks great cattle criticism advice, thank you!>

Comparative class constructor

  • In ES5 , define a constructor

// code demonstrates: 
function Star (name, Age) {
  the this .name = name;
  the this .age = Age; 
}

 

  • In ES6 , the definition of the class

// code demonstrates: 
class {Star constructor (name, Age) {
the this .name = name; the this .age = Age; } } console.dir (Star); // class written for ES6, ES5 only on the basis of forming a package, this method is called syntactic sugar

 

 


Static and instance members

  • Examples of members : this is the internal structure of the function by adding members. Examples members can only be accessed by instantiating objects.

// code demonstrates: 

function Star (the uname, Age) {
     the this .uname = the uname;
     the this .age = Age;
     the this .sing = function () { 
    the console.log ( 'I sing' ); 
    } 
} 

var LDH = new new Star ( 'Andy', 18 ); 
console.log (ldh.uname); // instance members can only instantiated object to access

 

 

  • Static member : add in the constructor of its own members. Static members can only be accessed via the constructor.

// code shows: 

function Star (uname, Age) {
  the this .uname = uname;
  the this .age = Age;
  the this .sing = function () { 
  console.log ( 'I can sing' ); 
  } 
} 

Star.sex = 'M' ;
 var ldh = new new Star ( 'Andy', 18 ); 
console.log (Star.sex); // static members can only be accessed via the constructor

 

 


Prototype prototype object constructor

  1. JavaScript provides each constructor has a prototype property, to a different object. Note that this prototype is an object, all properties and methods of this object will be owned by the constructor.
  2. Public property defines the constructor function, public way we put the prototype object body.

 


Prototype object

  1. The object will have a point to attribute __proto__ constructor prototype prototype object, the reason why we can use the properties and methods of the object constructor prototype prototype object, the object is because there exists __proto__ prototypes.
  2. __proto__ object prototype and the prototype is equivalent to the prototype object.
  3. __proto__ object prototype significance lies in providing a direction for the object lookup mechanism, or a route, but it is a non-standard property, so the actual development, can not use this property, it just points to the internal prototype object prototype

 


constructor Constructor

  1. Object prototype (__proto__) and constructors (prototype) prototype object which has a property constructor property, constructor we call the constructor, because it refers back to the constructor itself.
  2. constructor is mainly used to record the object reference to which constructor, which allows re-pointing to the original prototype object constructor.
  3. In general, the method of the object are provided in the prototype object constructor. If there are multiple object methods, we can take the form of an assignment to the object's prototype object, but this will overwrite the original content of the object constructor prototype, the prototype object constructor after such modifications will no longer point to the current constructor. In this case, we can modify the prototype object, add a point to the original constructor constructor.

 

// modify the original prototype object, the prototype object to the assignment of an object, you must manually using the constructor refers back to the original constructor. 
// code demonstrates: 

function Star (the uname, Age) {
     the this .uname = the uname;
     the this .age = Age; 
} 
// in many cases, we need to manually use this constructor property that points back to the original constructor 
Star.prototype = {
 // If we modified the original prototype object, the prototype object to the assignment of an object, you must manually using the constructor refers back to the original constructor 
constructor: Star, // set manually refer back to the original constructor 
    Sing: function () { 
        console.log ( 'I can sing' ); 
    }, 
    movie: function () { 
       console.log ( 'I will turn the movie'); 
    } 
} 
Var ZXY = new new Star ( 'Jacky', 19);  

 


Examples prototype object constructor and triangle

  1. constructor prototype property pointing prototype object constructor
  2. Examples of objects are created by the constructor, __proto__ attribute instance object points to the prototype object constructor
  3. properties of the prototype object constructor constructor constructor point, the prototype object instance constructor property also point constructor

 


Find the prototype chain mechanism and members

-> Any object has a prototype object, which is the prototype property, any prototype object is also an object that had proto property, so look up layer by layer, to form a chain, we call this the prototype chain

  • When accessing an object's properties (including methods), first locate the object itself does not have this property
  • If you do not look for it on the prototype (ie prototype prototype object __proto__ points)
  • If you do not find on the prototype (prototype object of Object) prototype object
  • And so on have been found so far Object (null)
  • __Proto__ significance lies in the object prototype for the object members to find a mechanism to provide direction, or a route

 

Guess you like

Origin www.cnblogs.com/carrot-cc/p/11129365.html