【发布时间】:2017-04-07 13:00:13
【问题描述】:
我正在尝试创建一个生成新实例的静态函数。实例属于静态函数所属的类。
这是一个例子
class A {
static getInstance() {
return new A();
}
}
到目前为止,let a = A.getInstance(); 可以正常工作。
我想在子类中继承此功能。
class B extends A {}
let b = B.getInstance(); // This will return an instance of A.
我希望B.getInstance() 返回一个 B 的实例。
【问题讨论】:
-
static getInstance() { return new this; }
标签: javascript node.js ecmascript-6 es6-class