這篇文章給大家分享的是有關(guān)ECMAScript 6中類繼承解析的示例的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

類繼承
看類繼承前,先回顧構(gòu)造函數(shù)怎么實(shí)現(xiàn)對象的繼承的
        function F() {
            this.a = 1;
        }
        function Son() {
            F.call(this);
        } 
        function inherit(S, F) {
            S.prototype = Object.create(F.prototype);
            S.prototype.constructor = S;
        }
        inherit(Son, F);
        let son = new Son();它實(shí)現(xiàn)了哪幾個功能:
繼承F的this屬性也就是F實(shí)例對象的屬性
Son.prototype.__proto__ === F.prototype 實(shí)現(xiàn)了上下輩分的繼承
son.constructor讓son認(rèn)祖歸宗
用來extends和super關(guān)鍵字,看一個簡單的繼承
        class A {
            constructor() {
                this.a = 1;
            }
        }
        class B extends A {
            constructor() {
                super();
                this.b = 2;
            }
            m() {
            }
        }
        let b = new B();同樣實(shí)現(xiàn)了那三點(diǎn)基本功能
B {a: 1, b: 2}
b.__proto__  == B.prototype
b.__proto__.__proto__ === A.prototype
b.constructor === B我認(rèn)為:關(guān)鍵字extends實(shí)現(xiàn)了原型的繼承,以及constructor的修正;關(guān)鍵字super實(shí)現(xiàn)了父類this的繼承,這里的super相當(dāng)于A.prototype.constructor.call(this)
寫了constructor,就必須在里面寫super,不然new子類實(shí)例對象會報錯;要么都不寫;其次子類的中constructor中的this屬性必須寫在super后面
1.ES5 的繼承,實(shí)質(zhì)是先創(chuàng)造子類的實(shí)例對象this,然后再將父類的方法添加到this上面(Parent.apply(this))。ES6 
的繼承機(jī)制完全不同,實(shí)質(zhì)是先將父類實(shí)例對象的屬性和方法,加到this上面(所以必須先調(diào)用super方法),然后再用子類的構(gòu)造函數(shù)修改this
2.因為子類自己的this對象,必須先通過父類的構(gòu)造函數(shù)完成塑造,得到與父類同樣的實(shí)例屬性和方法,然后再對其進(jìn)行加工,加上子類自己的實(shí)   例屬性和方法。如果不調(diào)用super方法,子類就得不到this對象。
        class B extends A {
            constructor() {    //要么都不寫,new時默認(rèn)會自動生成
                super();
                this.b = 2;    //寫在super后面
            } 
            m() {
            }
        }super作為函數(shù),只能放在子類的constructor中,指向A.prototype.constructor.call(this)
super作為對象,在子類普通方法中調(diào)用,super就是父類的原型也就是A.prototype;所以只能調(diào)用原型鏈上的方法,不能動用父類實(shí)例的方法和屬性constructor{}中的不能調(diào)用
        class A {
            constructor() {
                this.a = 1;
            }
            n() {
                return this;
            }
        }
        class B extends A {
            constructor() {
                
                super();
                this.b = 2;
                
            }
            m() {
                return super.n();
            }
        }
        let b = new B();
        b === b.m();并且規(guī)定
在子類普通方法中通過super調(diào)用父類的方法時,方法內(nèi)部的this指向當(dāng)前的子類實(shí)例。
所以上面return this 就是返回子類實(shí)例對象
super作為對象對屬性賦值時
super相當(dāng)于this,賦值屬性也就成了子類實(shí)例的屬性
class A {
  constructor() {
    this.x = 1;
  }
}
class B extends A {
  constructor() {
    super();
    this.x = 2;
    super.x = 3;
    console.log(super.x); // undefined
    console.log(this.x); // 3
    console.log(super.valueOf() instanceof B);   //true
  }
}
let b = new B();super作為對象,在靜態(tài)方法中指向的是父類能調(diào)用父類的靜態(tài)方法,如果方法內(nèi)部有this則指向當(dāng)前的子類
只有類才能調(diào)用類的靜態(tài)方法
        class A {
            constructor() {
                this.a = 1;
            }
            static n() {
                return this;
            }
        }
        class B extends A {
            constructor() {
                
                super();
                this.b = 2;
                
            }
            static m() {
                return super.n();
            }
        }
        console.log(A.n() === A)   // true
        console.log(B === B.m());  //true由于對象總是繼承其他對象的,所以可以在任意一個對象中,使用super關(guān)鍵字。
var obj = {
  toString() {
    return "MyObject: " + super.toString();
  }
};
Object.getPrototypeOf(obj).toString = function () {
    return "這里super等于obj.__proto__";
}
console.log(obj.toString());        //MyObject: 這里super等于obj.__proto__(1)子類的__proto__屬性,表示構(gòu)造函數(shù)的繼承,總是指向父類。
(2)子類prototype屬性的__proto__屬性,表示方法的繼承,總是指向父類的prototype屬性。
類的繼承模式
class A {
}
class B {
}
// B 的實(shí)例繼承 A 的實(shí)例
Object.setPrototypeOf(B.prototype, A.prototype);
// B 繼承 A 的靜態(tài)屬性
Object.setPrototypeOf(B, A);
const b = new B();也是因為這種實(shí)現(xiàn)所以類能調(diào)用自己的靜態(tài)方法
之前Array.apply(this)this并不會塑造Array里面的內(nèi)部結(jié)構(gòu),所以我們當(dāng)我們用類數(shù)組對象引用數(shù)組方法時用null代替了
而es6用類實(shí)現(xiàn)它的繼承,
代碼摘自es6入門
class MyArray extends Array {
  constructor(...args) {
    super(...args);
  }
}
var arr = new MyArray();
arr[0] = 12;
arr.length // 1
arr.length = 0;
arr[0] // undefined需要注意的是
ES6 改變了Object構(gòu)造函數(shù)的行為,一旦發(fā)現(xiàn)Object方法不是通過new Object()這種形式調(diào)用,ES6 規(guī)定Object構(gòu)造函數(shù)會忽略參數(shù)。
class NewObj extends Object{
  constructor(){
    super(...arguments);
  }
}
var o = new NewObj({attr: true});
o.attr === true  // false傳入?yún)?shù)會無效的
感謝各位的閱讀!關(guān)于“ECMAScript 6中類繼承解析的示例”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
                網(wǎng)站標(biāo)題:ECMAScript6中類繼承解析的示例-創(chuàng)新互聯(lián)
                
                文章分享:http://www.yijiale78.com/article44/pssee.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站收錄、品牌網(wǎng)站建設(shè)、小程序開發(fā)、Google、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容