javaScript (classes, relationships prototype, the prototype object, and in function object ES5)

Before ES6 specification does not define it, Js is actually a class constructor. (Quoted blog Park: heweiquan )

 1. A simple class.

function Person(){
    this.name='张三';
    this.age=20;
}
var p=new Person();
alert(p.name)

  

2. implementation inheritance through prototyping. (Prototype constructor and a method of increasing the inside chain)

the Person function () { 
    this.name = 'John Doe'; / * attribute * / 
    this.age = 20 is; 
    this.run = function () { 
        Alert (this.name + 'motion'); 
    } 
} 
// prototype chain the above properties are not shared by a plurality instance constructor 
Person.prototype.sex = "M"; 
Person.prototype.work = function () { 
    Alert (this.name + 'at work'); 
} 
var P = the Person new new (); 
//alert(p.name); 
//p.run (); 
p.work ();

 

3. class static method

the Person function () {} 
Person.getInfo = function () { 
    Alert ( 'static method I'); 
} 
// call the static method 
Person.getInfo ();

  

4.es5 posing inside of objects that inherit following the realization,

the Person function () { 
     this.name = 'John Doe'; / * attribute * / 
     this.age = 20 is; 
     this.run = function () {/ * Examples of methods * / 
         Alert (this.name + 'motion'); 
     } 
 }      
 Person.prototype.sex = "M"; 
 Person.prototype.work = function () { 
      Alert (this.name + 'at work'); 
} 
// Person class inherits the Web-based compositions prototype chain + inheritance patterns of objects posing 
the Web function () { 
    Person.call (the this); / * object masquerading inheritance * / 
} 
var new new the Web W = (); 
//w.run();// objects can inherit the posing constructor function properties and methods , where it is running 
w.work (); // inherited but not the prototype chain properties and methods of the above, it will be given here

  

5.es5 inside the prototype chain to inherit inheritance

the Person function () { 
    this.name = 'John Doe'; / * attribute * / 
    this.age = 20 is; 
    this.run = function () {/ * Examples of methods * / 
        Alert (this.name + 'motion'); 
    } 
}      
Person.prototype.sex = "M"; 
Person.prototype.work = function () { 
     Alert (this.name + 'at work'); 
} 
// Person class inherits the Web-based compositions prototype chain + inheritance patterns of objects posing 
the Web function () {} 
Web.prototype new new = the Person (); // prototype chain inheritance 
var new new the Web W = (); 
//w.run();// prototype chain inheritance: inherit constructor inside properties and methods, where it is running 
w.work (); // prototype chain may inherit attributes and methods of the above, it is here the normal operation

  

6. prototype inheritance chain problems

the Person function (name, Age) { 
    this.name = name; / * attribute * / 
    this.age = Age; 
    this.run = function () {/ * Examples of methods * / 
        Alert (this.name + 'motion'); 
    } 
} 
function the Web (name, Age) {} 
Web.prototype new new = the Person (); 
var new new the Web W = ( 'Zhao Si', 20 is); 
w.run (); // instantiable subclass when not parameter passing to the parent class, it will be given here
  

  

7. prototype chain combination + object inheritance patterns posing
the Person function (name, Age) { 
    this.name = name; / * attribute * / 
    this.age = Age; 
    this.run = function () {/ * Examples of methods * / 
        Alert (this.name + 'motion'); 
    } 
} 
function the Web (name, Age) { 
    Person.call (the this, name, Age); // instance subclasses inherit objects posing parameters can be transferred to the parent class 
} 
Web.prototype new new = the Person (); 
var new new = W Web ( 'Zhao Si', 20); 
w.run (); // here without error

 

8. Another way of posing the object prototype inheritance chain +

function Person(name,age){
    this.name=name;  /*属性*/
    this.age=age;
    this.run=function(){  /*实例方法*/
        alert(this.name+'在运动');
    }
}
function Web(name,age){
    Person.call(this,name,age);
}
Web.prototype=Person.prototype;
var w=new Web('赵四',20);
w.run();

  

 

The relationship between the function, Object object prototype, the prototype chain.

 First, understand what things construtor, as the name suggests is a constructor. Recall the code.

the Employee function (name, Job, Born) 
{ 
this.name = name; 
this.job = Job; 
this.born = Born; 
} 

var = new new Bill the Employee ( "Bill Gates", "Engineer", 1985); 

the console.log (bill.constructor); // Employee string describing the function. 
the console.log (Bill .__ proto__); {// constructor: fEmployee (name, Job, Born), proto __ __: Object} 

the console.log (bill.prototype); // undefined   only functions (including constructors) only prototype property . Ordinary objects no prototype (if not specified) 
console.log (the Employee .__ proto__ === Function.prototype) // to true

  

 Conclusion is:

                 1): We know that in Js, it is possible to use new place, only at 3.

                 Here is the first place. new + constructor. The second situation is new + Function (); third case is new + Object ().

                  A constructor object constructor to it.

             constructor Function.prototype object is Function,

              the object is the Object constructor Object.prototype

               

                  2): __proto__ prototype property of the object is used to access the constructor. Any object has __proto__ property.

 

Specific relationship is as follows:

 

Guess you like

Origin www.cnblogs.com/liuliu-hai/p/12549787.html