1.繼承
繼承方式:接口繼承(只繼承方法簽名)實(shí)現(xiàn)繼承(繼承實(shí)際的方法)
ECMAScript只支持實(shí)現(xiàn)繼承懈叹,并且主要是依靠原型鏈實(shí)現(xiàn)椭豫。
構(gòu)造函數(shù)首启,原型和實(shí)例的關(guān)系:每個(gè)構(gòu)造函數(shù)都有個(gè)原型對(duì)象嚷缭,原型對(duì)象都包含一直指向構(gòu)造函數(shù)的指針屡久,而實(shí)例都包含一個(gè)指向原型對(duì)象的內(nèi)部指針蝶桶。
原型鏈實(shí)現(xiàn)的基本模式:
functionn SuperType(){
this.property=true;
}
SuperType.prototype.getSuperValue=function(){
return this.property;
};
function SubType(){
this.subproperty=false;
}
SubType.prototype=new SuperType();
SubType.prototype.getSubValue=function(){
return this.subproperty;
};
var instance=new SubType();
alert(instance.getSuperValue());
上面的代碼定義了兩個(gè)類型:SuperType和SubType慨绳,SubType繼承了SuperType,通過創(chuàng)建一個(gè)SuperType的實(shí)例真竖,實(shí)現(xiàn)的本質(zhì)是重寫SubType的原型對(duì)象脐雪。
1.所有函數(shù)的默認(rèn)原型是Object
2.謹(jǐn)慎的定義方法:給原型添加的代碼一定要在替換原型的語句之后
function SuperType(){
this.property=true;
}
SuperType.prototype.getSuperValue=function(){
return this.property;
};
function SubType(){
this.subproperty=false;
}
SubType.prototype=new SuperType();//繼承SuperType,這里SuperType的實(shí)例 替換了SubType的原型疼邀,接著再定義下面的兩個(gè)方法
//添加新方法
SubType.prototype.getSubValue=function(){
return this.subproperty;
}
//重寫超類型中的方法
SubType.prototype.getSuperValue=function(){
return false;
}
var instance1=new SubType();
alert(instance1.getSuperValue());//false
var instance2=new SuperType();
alert(instance2.getSuperValue());//true
通過原型鏈實(shí)現(xiàn)繼承時(shí)喂江,不能使用對(duì)象自變量創(chuàng)建原型方法
function SuperType(){
this.property=true;
}
SuperType.prototype.getSuperValue=function(){
return this.property;
};
function SubType(){
this.subproperty=false;
}
SubType.prototype=new SuperType();//繼承SuperType
//添加新方法
SubType.prototype={
getSubValue:function(){
return this.subproperty;
},
someOtherMethod:function(){
return false;
}
};
var instance1=new SubType();
alert(instance1.getSuperValue());//error
這里面,SubType和SuperType之間沒有關(guān)系
2.原型鏈存在的問題
a.主要問題來自包含引用值類型的原型旁振;包含引用類型值的原型屬性會(huì)被所有實(shí)例共享
b.創(chuàng)建子類型的實(shí)例時(shí)获询,不能向超類型的構(gòu)造函數(shù)中傳遞參數(shù)
具體參考原型相關(guān)(一)的原型模式的問題
function SuperType(){
this.colors=["red","blue","gray"];
}
function SubType(){}
SubType.prototype=new SuperType();
var instance1=new SubType();
instance1.colors.push("black");
alert(instance1.colors);//"red,blue,gray,black"
var instance2=new SubType();
alert(instance2.colors);//"red,blue,gray,black"
3.因此要借用構(gòu)造函數(shù)
基本思想:在子類型構(gòu)造函數(shù)的內(nèi)部調(diào)用超類型的構(gòu)造函數(shù)
function SuperType(){
this.colors=["red","blue","gray"];
}
function SubType(){
SuperType.call(this);//醬紫繼承了SuperType
}
var instance1=new SubType();
instance1.colors.push("black");
alert(instance1.colors);//"red,blue,gray,black"
var instance2=new SubType();
alert(instance2.colors);//"red,blue,gray"
上面的注釋行涨岁,在新創(chuàng)建的SubType實(shí)例的環(huán)境下借調(diào)了超類型的構(gòu)造函數(shù),這樣吉嚣,在新SubType對(duì)象上執(zhí)行SuperType()函數(shù)中定義的所有對(duì)象初始化代碼梢薪,因此每個(gè)對(duì)象就會(huì)擁有寄幾的colors副本啦
傳遞參數(shù)
function SuperType(name){
this.name=name;
}
function SubType(){
SuperType.call(this,"Andy");//醬紫繼承了SuperType
this.age=20;//實(shí)例屬性
}