javascript 寄生組合繼承

 

今天剛學完,還沒弄懂.函數

//臨時中轉函數
function obj(o){
function F(){};
F.prototype=o;
return new F();
}this

//寄生函數
function create(box,desk){
var f=obj(box.prototype);
f.constructor = desk;
desk.prototype=f;

}prototype

//構造函數
function Box(name,age){
this.name=name;
this.age=age;
}對象

Box.prototype.run=function(){
return this.name+this.age+'運行中..';
}io

//對象冒充
function Desk(name,age){
Box.call(this,name,age);
}function

create(Box,Desk); //用來替代Desk.prototype=new Box();構造函數

var desk=new Desk('lee',100);
alert(desk.run());call

相關文章
相關標籤/搜索