js prototypal inheritance in several ways

1. prototype chain to inherit

2, inherited constructor (posing the object inheritance)

3, a combination of inherited (+ prototype chain Inherited constructor inherited)

4, prototypal inheritance

5. parasitic combined inheritance

One. Prototype inheritance chain

the Show function () {
 the this .name = " RUN " ; 
} 

function the Run () { 
the this .age = " 20 is " ; // the Run inherited Show, through the prototype, forming a chain 
} 
Run.prototype = new new the Show ();
 var = Show new new the Run (); 
Alert (show.name) // results: run

two. Constructors Inheritance (object masquerading inherited)

As a reference to solve shared problems and super-types can not pass the Senate, we use a technique called borrow constructor, or
who become object masquerading ( counterfeit objects, classical inheritance ) technology to solve these two problems 

function Box (Age) {
 the this .name = [ 'Lee', 'Jack', 'the Hello' ]
 the this .age = Age; 
} 
function Desk (Age) { 
Box.call ( the this , Age); // object masquerading, parameter passing to a supertype 
}
 var Desk = new new Desk (200 is ); 
Alert (desk.age); // 200 is 
Alert (desk.name); // [ 'Lee', 'Jack', 'the Hello'] 
desk.name .push ( 'the AAA'); // new data is added only to Desk 
Alert (desk.name) // [ 'Lee', 'Jack', 'the Hello', 'the AAA']

three. Inheritance composition (Inherited prototype chain inherited constructor +)

Borrowing Constructors while solving both of these problems just now, but there is no prototype, reuse is out of the question. So, we need
to borrow the prototype chain + mode constructor, this model has become a combination of inheritance.

function Box(age) {
this.name = ['Lee', 'Jack', 'Hello']
this.age = age;
}
Box.prototype.run = function () {
return this.name + this.age;
};
function Desk(age) {
Box.call(this, age); //对象冒充
}
Desk.prototype = new Box(); //原型链继承
var desk = new Desk(100);
alert(desk.run());

four. Prototypal inheritance

With this prototype inheritance and create new objects based on existing objects,
but also do not have to therefore create a custom type 

function obj (O) { // pass a function literal 
function F. () {} // create a constructor 
F.prototype = O; // the literal assigned to the constructor function prototype 
return  new new F. (); / / eventually return an instance constructors 
}
 var Box = { // object literal 
name: 'Lee' , 
ARR: [ 'brother', 'sister', 'sister' ] 
}; 
var box1 obj = (Box); // pass 
Alert (box1.name); 
box1.name = 'Jack' ; 
Alert (box1.name); 
Alert (box1.arr); 
box1.arr.push ( 'parent' ); 
Alert (box1.arr);
var BOX2 obj = (Box); // transfer 
Alert (box2.name); 
Alert (box2.arr); // reference type shared

Fives. Combined parasitic inheritance

Parasitic combined inherited solve the problem of two calls, there will be a combined inheritance case two calls

The basic model is as follows:

function Object (O) {
     function F. () {} 
    F.prototype = O;
     return  new new F. (); 
} 

function inheritPrototype (subType, Supertype) {
     var the prototype = Object (superType.prototype);   // Create Object 
    prototype.constructor subType =;               // augmented object 
    subType.prototype = the prototype;                 // specified object 
}

I only know that these types but also did not get to know. . . . = So embarrassed. =

Guess you like

Origin www.cnblogs.com/LWWTT/p/11100210.html