您可以添加
static
方法create,从类原型创建对象。类似的事情应该管用:
class Test {
constructor(foo) {
this.foo = 'test';
}
static create() {
return Object.create(this.prototype);
}
}
const a = new Test('bar'); // call constructor
const b = Test.create(); // do not call constructor
console.log(a.foo, a instanceof Test); // bar, true
console.log(b.foo, a instanceof Test); // undefined, true