ES6 class中的super

class?A?{

????constructor()?{

??????this.x?=?1;

??????this.print=()=>{

??????????console.log(this.x+'A?instance');

??????}

????}

????static?x=2

????static?print()?{

????????console.log(this.x+'A?static');

????}

????print()?{

????????console.log(this.x+'A?prototype');

????}

??}

??A.prototype.x=3;

??class?B?extends?A?{

????constructor()?{

??????super();

??????this.x?=?9;

??????//super.x?=?10;

??????this.m=()=>{super.print()};

??????//console.log(super.x);?//?undefined

??????//console.log(this.x);?//?3

????}

????static?x=20

????static?m()?{

????????super.print();

????}

????m()?{

????????super.print();

??????}

??}

??B.prototype.x=30;


??let?b?=?new?B();

??b.m();

??B.m();

??B.prototype.m();



prototype就是用來溯源的;this看引用的對(duì)象;兩者應(yīng)分開查找。

?著作權(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)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 在ES6中使用class實(shí)現(xiàn)繼承,子類必須在constructor方法中調(diào)用super方法,否則新建實(shí)例時(shí)會(huì)報(bào)錯(cuò)。...
    8d2855a6c5d0閱讀 5,083評(píng)論 1 8
  • super這個(gè)關(guān)鍵字,既可以當(dāng)作函數(shù)使用,也可以當(dāng)作對(duì)象使用。在這兩種情況下,它的用法完全不同。 1、當(dāng)作函數(shù)使用...
    是嚶嚶嚶呀閱讀 374評(píng)論 0 0
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,153評(píng)論 0 2
  • 簡(jiǎn)介 Class可以通過extends關(guān)鍵字實(shí)現(xiàn)繼承。 上面代碼定義了一個(gè)ColorPoint類,該類通過exte...
    oWSQo閱讀 761評(píng)論 0 1
  • 僅為方便個(gè)人查詢使用來源:http://es6.ruanyifeng.com/#docs/class-extend...
    zqyadam閱讀 335評(píng)論 0 0

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