ES6 classes: is it possible to access the constructor of a child class from the parent?

十年热恋 提交于 2020-08-24 10:38:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!