<分区>
这与 an old question 有关但我特别询问有关使用编写函数的旧方法扩展新的 ES6 类的问题。
以下代码无效。
class X {
a;
constructor(a) {
this.a = a;
}
}
X.call({}, 3); // Uncaught TypeError: Class constructor X cannot be invoked without 'new'
有没有办法间接调用构造函数?比如我是不是可以用老套的原型(prototype)继承来做一个Y?
function Y(a) {
X.call(this, a); // this doesn't work here, can't use super as well
}
Y.prototype = Object.create(X.prototype);
Object.setPrototypeOf(Y, X);
const y = new Y(12);