Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 460
mkrause Avatar asked Feb 10 '17 14:02

mkrause


1 Answers

You can access the current instance's constructor via this.constructor.

like image 121
Felix Kling Avatar answered Oct 13 '22 03:10

Felix Kling