转圈圈

JavaScript继承

目录

  1. es5 构造函数
  2. es6 class

es5

function A() {
  this.a = 1;
}

function B() {
  this.b = 2;
}

function C() {
  A.call(this);
  B.call(this);
  this.c =3;
}

C.prototype = Object.create(A.prototype);
Object.assign(C.prototype,B.prototype);

C.prototype.constructor = C;

var c = new C();

es6 class

class A { 
  constructor() {
    this.x =1;
  }
  print() {
    console.log(this.x);
  }
}

class B extends A {
  constructor() {
    super();
    this.x =2;
  }
  m() {
    super.print();
  }
}

let b = new B();
posted @ 2019-05-18 10:30  rosendolu  阅读(84)  评论(0编辑  收藏  举报