JavaScript組合繼承詳解
1、前言
首先學(xué)習(xí)繼承之前,要對原型鏈有一定程度的了解。
不了解可以去先閱讀我另一篇文章,里面對原型鏈有一個較為詳細(xì)的說明:JavaScript 原型鏈詳解。
如果已經(jīng)了解請繼續(xù)。
之前寫過一篇博文將繼承方式全部列出來了,不過我發(fā)現(xiàn)一口氣看完過于長了,也不利于吸收知識,所以我先將組合繼承部分劃分出來,后續(xù)會把寄生部分補上。
2、原型鏈繼承
父類實例作為子類的原型
子類創(chuàng)造的兩個實例的隱式原型__proto__指向父類的那個實例
而父類的實例的隱式原型__proto__又指向父類的原型father.prototype
根據(jù)原型鏈的特性,所有子類的實例能夠繼承父類原型上的屬性。
閱覽以下這張圖可以配合代碼理解清晰:

//父類
function father() {
this.fatherAttr = ["fatherAttr"];
}
//父類的原型上的屬性
father.prototype.checkProto = "checkProto";
//子類
function child() {}
// 將father實例作為child這個構(gòu)造函數(shù)的原型
child.prototype = new father();
child.prototype.constructor = child;
//兩個子類實例
const test1 = new child();
const test2 = new child();
console.log("測試1:");
console.log("test1:", test1);
console.log("test2:", test2);
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("測試2:");
test1.fatherAttr.push("newAttr");
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("測試3:");
console.log("test1.checkProto:", test1.checkProto);

特點:
- 兩個實例對象都沒有
fatherAttr屬性,但是因為父類的實例會擁有fatherAttr屬性,且現(xiàn)在父類的實例作為child的原型,根據(jù)原型鏈,他們可以共享到自己的構(gòu)造函數(shù)child的原型上的屬性。(測試1) - 因為只有一個父類的實例作為他們的原型,所以所有實例共享了一個原型上的屬性
fatherAttr,當(dāng)原型上的屬性作為引用類型時,此處是數(shù)組,test1添加一個新內(nèi)容會導(dǎo)致test2上的fatherAttr也改變了。(測試2)(缺點) child構(gòu)造函數(shù)不能傳遞入?yún)?。(缺點)- 實例可以訪問到父類的原型上的屬性,因此可以把可復(fù)用方法定義在父類原型上。(測試3)
3、構(gòu)造函數(shù)繼承
將父類上的this綁定到子類,也就是當(dāng)子類創(chuàng)造實例時會在子類內(nèi)部調(diào)用父類的構(gòu)造函數(shù),父類上的屬性會拷貝到子類實例上,所以實例會繼承這些屬性。
//父類
function father(params) {
this.fatherAttr = ["fatherAttr"];
this.params = params;
}
//父類的原型上的屬性
father.prototype.checkProto = "checkProto";
//子類
function child(params) {
father.call(this, params);
}
//兩個子類實例
const test1 = new child("params1");
const test2 = new child("params2");
console.log("測試1:");
console.log("test1:", test1);
console.log("test2:", test2);
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("測試2:");
test1.fatherAttr.push("newAttr");
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("測試3:");
console.log("test1.checkProto:", test1.checkProto);

特點:
- 兩個實例對象都擁有了拷貝來的
fatherAttr屬性,所以沒有共享屬性,創(chuàng)造一個實例就得拷貝一次父類的所有屬性,且因為不能繼承父類原型,所以方法不能復(fù)用,被迫拷貝方法。(測試1)(缺點) test1添加一個新內(nèi)容只是改變了test1自己的屬性,不會影響到test2。(測試2)child構(gòu)造函數(shù)可以傳遞參數(shù),定制自己的屬性。(測試1)- 實例不能繼承父類的原型上的屬性。(測試3)(缺點)
4、組合繼承
結(jié)合原型鏈繼承和構(gòu)造函數(shù)繼承,可以根據(jù)兩種繼承特點進(jìn)行使用。
//父類
function father(params) {
this.fatherAttr = ["fatherAttr"];
this.params = params;
}
//父類的原型上的屬性
father.prototype.checkProto = "checkProto";
//子類
function child(params) {
//第二次調(diào)用了父類構(gòu)造函數(shù)
father.call(this, params);
}
// 將father實例作為child構(gòu)造函數(shù)的原型
child.prototype = new father();//第一次調(diào)用了父類構(gòu)造函數(shù)
child.prototype.constructor = child;
//兩個實例
const test1 = new child("params1");//從這里跳轉(zhuǎn)去子類構(gòu)造函數(shù)第二次調(diào)用了父類構(gòu)造函數(shù)
const test2 = new child("params2");
console.log("測試1:");
console.log("test1:", test1);
console.log("test2:", test2);
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("測試2:");
test1.fatherAttr.push("newAttr");
console.log("test1.fatherAttr:", test1.fatherAttr);
console.log("test2.fatherAttr:", test2.fatherAttr);
console.log("測試3:");
console.log("test1.checkProto:", test1.checkProto);
console.log("測試4:");
delete test1.fatherAttr
console.log("test1:", test1);
console.log("test1.fatherAttr:", test1.fatherAttr);

特點:
- 兩個實例對象都擁有了拷貝來的
fatherAttr屬性,創(chuàng)造一個實例就得拷貝一次父類的所有屬性(構(gòu)造函數(shù)繼承特點,測試1),但是能訪問父類原型,可以把復(fù)用方法定義在父類原型上。(原型鏈繼承特點,測試1) test1添加一個新內(nèi)容只是改變了test1自己的屬性,不會影響到test2。(構(gòu)造函數(shù)繼承特點,測試2)child構(gòu)造函數(shù)可以傳遞參數(shù),定制自己的屬性。(構(gòu)造函數(shù)繼承特點,測試1)- 實例能繼承父類的原型上的屬性。(原型鏈繼承特點,測試3)
- 調(diào)用了兩次父類的構(gòu)造函數(shù),生成兩份實例,創(chuàng)建子類原型鏈一次,用子類創(chuàng)建實例時,子類內(nèi)部里面一次,第二次覆蓋了第一次。(缺點)
- 因為調(diào)用兩次父類構(gòu)造函數(shù),如果用delete刪除實例上拷貝來的
fatherAttr屬性,實例仍然擁有隱式原型指向的父類實例上的fatherAttr屬性。(原型鏈繼承特點,測試4)(缺點)
到此這篇關(guān)于JavaScript組合繼承詳解的文章就介紹到這了,更多相關(guān)JavaScript組合繼承內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
讓chatGPT教你如何使用taro創(chuàng)建mbox
這篇文章主要為大家介紹了讓chatGPT教你如何使用taro創(chuàng)建mbox實現(xiàn)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Vue.js React與Angular流行前端框架優(yōu)勢對比
這篇文章主要為大家介紹了Vue.js React與Angular流行前端框架優(yōu)勢對比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

