js的繼承

1.原型鏈繼承

核心: 將父類(lèi)的實(shí)例作為子類(lèi)的原型
缺點(diǎn):
1.要想為子類(lèi)新增原型屬性和方法,必須要在new Parent()這樣的語(yǔ)句之后執(zhí)行
2.無(wú)法實(shí)現(xiàn)多繼承。
3.來(lái)自父類(lèi)原型對(duì)象的所有屬性被所有實(shí)例共享
4.創(chuàng)建子類(lèi)實(shí)例時(shí),無(wú)法向父類(lèi)構(gòu)造函數(shù)傳參。

function Parent(age) {
    this.age = age;
};
Parent.prototype.friends = ['a', 'b'];
Parent.prototype.getAge = function () {    //對(duì)原型進(jìn)行擴(kuò)展
    return this.age;
};

function Child(sex) {
    this.sex = sex;
};

Child.prototype = new Parent(29);       //這一句是關(guān)鍵  //通過(guò)構(gòu)造器函數(shù)創(chuàng)建出一個(gè)新對(duì)象,把老對(duì)象的東西都拿過(guò)來(lái)。

Child.prototype.getSex = function () {
    return this.sex;
};

// Child.prototype.getAge = function () {   //可以重寫(xiě)從父類(lèi)繼承來(lái)的方法,會(huì)優(yōu)先調(diào)用自己的。
//     console.log(222);
// };

var result = new Child('male');
var temp = new Child('female');
temp.friends.push('c');         
console.log(result.friends);    // ['a', 'b', 'c']

console.log(result.getSex());   //  output: male    //調(diào)用了從 Parent 原型中繼承來(lái)的方法(繼承到了當(dāng)前對(duì)象的原型中)  
console.log(result.getAge());   //  output: 29      //調(diào)用了從 Child 原型中擴(kuò)展來(lái)的方法

2.構(gòu)造繼承

基本思想: 借用構(gòu)造函數(shù)的基本思想就是利用call或者apply把父類(lèi)中通過(guò)this指定的屬性和方法復(fù)制(借用)到子類(lèi)創(chuàng)建的實(shí)例中.
因?yàn)閠his對(duì)象是在運(yùn)行時(shí)基于函數(shù)的執(zhí)行環(huán)境綁定的.也就是說(shuō),在全局中, this等于window,而當(dāng)函數(shù)被作為某個(gè)對(duì)象的方法調(diào)用時(shí), this等于那個(gè)對(duì)象.
call、apply方法可講一個(gè)函數(shù)的底箱上下文從初始的上下文改變?yōu)橛?thisObj指定的新對(duì)象.

所以,這個(gè)借用構(gòu)造函數(shù)就是, new對(duì)象的時(shí)候(new創(chuàng)建的時(shí)候, this指向創(chuàng)建的這個(gè)實(shí)例), 創(chuàng)建了一個(gè)新的實(shí)例對(duì)象,
并且執(zhí)行Child里面的代碼, 而Child里面用call調(diào)用了Parent,也就是說(shuō) 把this指向改成了指向新的實(shí)例,
所以就會(huì)把Child里面的this縣官屬性和方法復(fù)制到新的實(shí)例上, 而不是賦值到Parent上面,所以所有實(shí)例中就擁有了父類(lèi)定義的這些this的屬性和方法.
因?yàn)閷傩允墙壎ǖ絫his上面的, 所以調(diào)用的時(shí)候才賦到相應(yīng)的實(shí)例中, 各個(gè)實(shí)例的值就不會(huì)互相影響了.

核心: 使用父類(lèi)構(gòu)造函數(shù)來(lái)增強(qiáng)子類(lèi)實(shí)例, 等于是賦值父類(lèi)的實(shí)例屬性給子類(lèi)(沒(méi)用到原型);
缺點(diǎn): 方法都在構(gòu)造函數(shù)中定義, 只能繼承父類(lèi)的實(shí)例屬性和方法, 不能繼承原型屬性/方法, 無(wú)法實(shí)現(xiàn)函數(shù)復(fù)用, 每個(gè)子類(lèi)都有父類(lèi)實(shí)例函數(shù)的副本, 影響性能

function Parent(age){
    this.age = age;
    this.friends = ['a', 'b'];
    this.getAge = function() {
        return this.age;
    }
}

Parent.prototype.consoleLog = function () { // 對(duì)原型進(jìn)行擴(kuò)展的方法就無(wú)法復(fù)用了
    console.log('這是原型方法');
}

function Child(sex) {
    Parent.call(this, '29');    
    this.sex = sex;
}

var result = new Child('male');

console.log(result.age);                        //  29
console.log(result.friends);                    //  [a, b]
console.log(result.getAge());                   //  29
console.log(result.sex);                        //  male

console.log(result.consoleLog());               //  ?  TypeError: result.consoleLog is not a function

3.組合繼承

組合繼承(所有的實(shí)例都能擁有自己的屬性,并且可以使用相同的方法,組合繼承避免了原型鏈和借用構(gòu)造函數(shù)的缺陷,結(jié)合了兩個(gè)的優(yōu)點(diǎn),是最常用的繼承方式)

核心:通過(guò)調(diào)用父類(lèi)構(gòu)造,繼承父類(lèi)的屬性并保留傳參的優(yōu)點(diǎn),然后再通過(guò)將父類(lèi)實(shí)例作為子類(lèi)原型,實(shí)現(xiàn)函數(shù)復(fù)用
缺點(diǎn):調(diào)用了兩次父類(lèi)構(gòu)造函數(shù),生成了兩份實(shí)例(子類(lèi)實(shí)例將子類(lèi)原型上的那份屏蔽了)

function Parent(age) {
    this.age = age;
    this.friends = ['a', 'b'];
};

Person.prototype.getAge = function () {
    return this.age;
};

function Child(sex) {
    Parent.call(this, '29');  //這一步很關(guān)鍵
    this.sex = sex;
};

Child.prototype = new Parent('29');  //這一步也很關(guān)鍵
var result = new Child('male');

console.log(result.age);        // 29
result.friends.push("c");     
console.log(result.friends);    //  ['a','b','c']
console.log(result.getAge());   //  29
console.log(result.sex);     //  male

var result1 = new Child('female');   //通過(guò)借用構(gòu)造函數(shù)都有自己的屬性,通過(guò)原型享用公共的方法
console.log(result1.sex);     //female
console.log(result1.friends);  //['a','b']

4.寄生組合繼承

核心:通過(guò)寄生方式,砍掉父類(lèi)的實(shí)例屬性,這樣,在調(diào)用兩次父類(lèi)的構(gòu)造的時(shí)候,就不會(huì)初始化兩次實(shí)例方法/屬性,避免的組合繼承的缺點(diǎn)
缺點(diǎn):堪稱(chēng)完美,但實(shí)現(xiàn)較為復(fù)雜

function Parent(age) {
    this.age = age;
    this.friends = ['a','b'];
}

Parent.prototype.getAge = function () {
    return this.age;
};

function Child(sex) {
    Parent.call(this, '29');
    this.sex = sex;
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

var result = new Child('male');

console.log(result.age);
console.log(result.friends);
console.log(result.getAge());
console.log(result.sex);
console.log(result.constructor)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過(guò)簡(jiǎn)信或評(píng)論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

  • 這是16年5月份編輯的一份比較雜亂適合自己觀(guān)看的學(xué)習(xí)記錄文檔,今天18年5月份再次想寫(xiě)文章,發(fā)現(xiàn)簡(jiǎn)書(shū)還為我保存起的...
    Jenaral閱讀 3,143評(píng)論 2 9
  • (一)、call()方法1.語(yǔ)法:call([ thisobj[ arg1 [ arg2 [ argN ] ] ]...
    木子川頁(yè)心閱讀 276評(píng)論 0 0
  • JS作為面向?qū)ο蟮娜躅?lèi)型語(yǔ)言,繼承也是其非常強(qiáng)大的特性之一。那么如何在JS中實(shí)現(xiàn)繼承呢?讓我們拭目以待。 JS繼承...
    依依玖玥閱讀 293評(píng)論 0 2
  • (一)、call()方法 1.語(yǔ)法:call([ thisobj[ arg1 [ arg2 [ argN ] ] ...
    李丹linda閱讀 228評(píng)論 0 2
  • ??面向?qū)ο螅∣bject-Oriented,OO)的語(yǔ)言有一個(gè)標(biāo)志,那就是它們都有類(lèi)的概念,而通過(guò)類(lèi)可以創(chuàng)建任意...
    霜天曉閱讀 2,256評(píng)論 0 6

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