简体   繁体   中英

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

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.

您可以通过this.constructor访问当前实例的构造函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM