繼承復(fù)習(xí)

1、原型鏈---繼承通過創(chuàng)建父類的實例。本質(zhì)上市重寫原型對象

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());

使用字面量添加的新方法,會使,繼承一行的代碼無效。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"

相對于原型鏈的優(yōu)勢即可以在子類型構(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ù)的缺點。

4、原型式繼承--借助原型可以基于已有的對象創(chuàng)建新對象

function object(o){
    function F(){}
    F.prototype = o;
    return new F();
}

在object函數(shù)內(nèi)部先創(chuàng)建一個臨時的構(gòu)造函數(shù),然后將傳入的對象作為這個構(gòu)造函數(shù)的原型,最后返回這個臨時類型的一個新實例。
從本質(zhì)上講,object()對其傳入其中的對象執(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)建一個僅用于封裝繼承過程的函數(shù)。該函數(shù)內(nèi)部以某種方式來增強對象,最后再像真地是它做了所有工作一樣返回對象。

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()不是必須的,只要能夠返回新對象的函數(shù)都適用于此模式。

6、寄生組合式繼承--組合式繼承問題就是無論在什么情況下,都會調(diào)用兩次超類型構(gòu)造函數(shù)。一次是在創(chuàng)建子類型原型的時候。另一次在子類型內(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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容