1、原型鏈---繼承通過創(chuàng)建父類的實(shí)例贸铜。本質(zhì)上市重寫原型對(duì)象
function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
}
function SubType(){
this.subproperty = false;
}
//繼承了SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function(){
return this.subproperty;
}
var instance = new SubType();
alert(instance.getSubValue());
使用字面量添加的新方法堡纬,會(huì)使,繼承一行的代碼無效蒿秦。constructor指向變了烤镐。
SubType.prototype = new SuperType();
SubType.prototype = {
getSubValue:function(){},
getName:function(){}
}
2、借用構(gòu)造函數(shù)---即在子類型構(gòu)造函數(shù)內(nèi)部調(diào)用超類型構(gòu)造函數(shù)
function SuperType(){
this.colors = ["red","blue","green"];
}
function SubType(){
//繼承了SuperType
SuperType.call(this);
}
var instance = new SubType();
instance.colors.push("black");
alert(instance.colors);//"red,blue,green,black"
var instance2 = new SubType();
alert(instance2.colors);//"red,blue,green"
相對(duì)于原型鏈的優(yōu)勢(shì)即可以在子類型構(gòu)造函數(shù)中向超類型構(gòu)造函數(shù)中傳遞參數(shù)call()使用
問題:方法都在構(gòu)造函數(shù)中定義棍鳖。
3炮叶、組合繼承---原型鏈和構(gòu)造函數(shù)技術(shù)組合到一起
function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function (){
return this.name;
}
function SubType(name,age){
//繼承屬性
SuperType.call(this,name);
this.age = age;
}
//繼承方法
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
return this.age;
}
var instance1 = new SubType("Nicholas",20);
instance1.colors.push("black");
alert(instance1.colors);//"red,blue,green,black"
instance1.sayName();//"Nicholas"
instance1.sayAge();//20
var instance2 = new SubType("Jemmy",30);
alert(instance2.colors);//"red,blue,green"
instance2.sayName();//"Jemmy"
instance2.sayAge();//30
//避免了原型鏈和構(gòu)造函數(shù)的缺點(diǎn)。
4、原型式繼承--借助原型可以基于已有的對(duì)象創(chuàng)建新對(duì)象
function object(o){
function F(){}
F.prototype = o;
return new F();
}
在object函數(shù)內(nèi)部先創(chuàng)建一個(gè)臨時(shí)的構(gòu)造函數(shù)悴灵,然后將傳入的對(duì)象作為這個(gè)構(gòu)造函數(shù)的原型扛芽,最后返回這個(gè)臨時(shí)類型的一個(gè)新實(shí)例。
從本質(zhì)上講积瞒,object()對(duì)其傳入其中的對(duì)象執(zhí)行了一次淺復(fù)制
var person = {
name:"Nicholas",
fridens:["Shelby","Court","Van"]
};
var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.fridens.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.fridens.push("Barbie");
alert(person.fridens);//""Shelby,Court,Van,Rob,Barbie"
ECMAScript5通過熙增Object.create()方法規(guī)范了原型式繼承與上面的object()行為相同川尖。
var person = {
name:"Nicholas",
fridens:["Shelby","Court","Van"]
};
var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.fridens.push("Rob");
var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.fridens.push("Barbie");
alert(person.fridens);//""Shelby,Court,Van,Rob,Barbie"
5、寄生式繼承--創(chuàng)建一個(gè)僅用于封裝繼承過程的函數(shù)茫孔。該函數(shù)內(nèi)部以某種方式來增強(qiáng)對(duì)象叮喳,最后再像真地是它做了所有工作一樣返回對(duì)象。
function createAnother(original){
var clone = create(original);
clone.sayHi = function(){
alert("HI");
};
return clone;
}
var person = {
name:"Nicholas",
fridens:["Shelby","Court","Van"]
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();//"HI"
//create()不是必須的缰贝,只要能夠返回新對(duì)象的函數(shù)都適用于此模式馍悟。
6、寄生組合式繼承--組合式繼承問題就是無論在什么情況下剩晴,都會(huì)調(diào)用兩次超類型構(gòu)造函數(shù)锣咒。一次是在創(chuàng)建子類型原型的時(shí)候。另一次在子類型內(nèi)部
寄生組合式繼承即通過借用構(gòu)造函數(shù)來繼承屬性赞弥,通過原型鏈混成形式來繼承方法毅整,本質(zhì):不必為了指定子類型的原型而調(diào)用超類型的構(gòu)造函數(shù)
function inheritProrotype(SubType,SuperType){
var prototype = create(SuperType.prototype);
prototype.constructor = SubType;
SubType.prototype = prototype;
}