Inheriting with object impersonation, only the information in the constructed object can be inherited
A method in a prototype that cannot inherit constructs an object that allocates space each time it instantiates
Cause wasted space
function Box (name,age) {
This.name=name;
This.age=age;
This.run=function () {
Return this.name+this.age+ "Running ..."
}
}
Box.prototype.family= ' Plus ';
function Desk (name,age) {
Box.call (This,name,age);
}
var desk=new desk (' Lee ', 100);
Alert (Desk.run ());
Prototype chain + constructor mode, called combinatorial mode
function Box (name,age) {
This.name=name;
This.age=age;
}
Box.prototype.run=function () {
Return this.name+this.age+ "in Run ..."
}
function Desk (name,age) {
Box.call (this,name,age);//Object impersonating
}
Desk.prototype=new Box ();//prototype chain inheritance
var desk=new desk (' Lee ', 100);
Alert (Desk.run ());
JavaScript combination mode, object impersonation + prototype chain inheritance