如何使用ES5扩展ES6类?

Mar*_*ahn 6 javascript ecmascript-5 ecmascript-6

这样做的原因很复杂,但归结为流不理解mixins或任何其他修改ES6类原型的方法.所以我回到了ES5,但我无法弄清楚如何在没有的情况下调用ES6类的构造函数new:

class A {
  constructor() {}
}

function B() {
  // what do I put here?  I would do something like
  // A.prototype.constructor.call(this) but that throws an error saying the
  // constructor can only be called with `new`
}
B.prototype = Object.create(A.prototype);
Run Code Online (Sandbox Code Playgroud)

小智 2

我自己回答这个问题:

class A {
  constructor() {}
}

function B() {
  Object.assign(this, new A());
}
B.prototype = Object.create(A.prototype);
Run Code Online (Sandbox Code Playgroud)

不确定这里是否有任何副作用