如何使用 ES5 扩展 ES6 class?
How do I extend an ES6 class with ES5?
这样做的原因很复杂,但归根结底是因为流程不理解混入或任何其他修改 ES6 class 原型的方式。所以我又回到了 ES5,但我不知道如何在没有 new
:
的情况下调用 ES6 class 的构造函数
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);
我自己回答:
class A {
constructor() {}
}
function B() {
Object.assign(this, new A());
}
B.prototype = Object.create(A.prototype);
不确定这里有没有副作用
这样做的原因很复杂,但归根结底是因为流程不理解混入或任何其他修改 ES6 class 原型的方式。所以我又回到了 ES5,但我不知道如何在没有 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);
我自己回答:
class A {
constructor() {}
}
function B() {
Object.assign(this, new A());
}
B.prototype = Object.create(A.prototype);
不确定这里有没有副作用