问题
Using ES6 class syntax, is it possible to create a new instance of the current class from the parent? For example:
class Base {
withFoo() {
return new self({ foo: true });
}
}
class Child extends Base {}
(new Child()).withFoo();
I'm looking for something similar to PHP's new self()
syntax.
回答1:
You can access the current instance's constructor via this.constructor
.
来源:https://stackoverflow.com/questions/42161828/es6-classes-is-it-possible-to-access-the-constructor-of-a-child-class-from-the