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看引用的对象;两者应分开查找。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在ES6中使用class实现继承,子类必须在constructor方法中调用super方法,否则新建实例时会报错。...
    8d2855a6c5d0阅读 10,367评论 1 8
  • super这个关键字,既可以当作函数使用,也可以当作对象使用。在这两种情况下,它的用法完全不同。 1、当作函数使用...
    是嘤嘤嘤呀阅读 2,635评论 0 0
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,182评论 0 2
  • 简介 Class可以通过extends关键字实现继承。 上面代码定义了一个ColorPoint类,该类通过exte...
    oWSQo阅读 3,933评论 0 1
  • 仅为方便个人查询使用来源:http://es6.ruanyifeng.com/#docs/class-extend...
    zqyadam阅读 2,312评论 0 0

友情链接更多精彩内容