使用ES6类语法,是否可以从父类创建当前类的新实例?例如:
class Base {
withFoo() {
return new self({ foo: true });
}
}
class Child extends Base {}
(new Child()).withFoo();
我正在寻找类似于PHPnew self()
语法的内容。
您可以通过访问当前实例的构造函数this.constructor
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句