简体   繁体   English

ES6 类:是否可以从父类访问子类的构造函数?

[英]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?使用 ES6 类语法,是否可以从父类创建当前类的新实例? 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.我正在寻找类似于 PHP 的new self()语法的东西。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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