How to instantiate a child class from static method of parent class in javascript es6

I looked for the way to get current class in static method of the base class,
to create the correct instance, depending on actual class, this static method was called on, but found nothing.
Luckily I discovered, that ‘this’ is a constructor but not an instance in a static method,
so you can just call it with ‘new’, in case you want something like factory:

class Base {
    return `Hello from ${}!`;
  static getInstance(){
    console.log( + ' creating..');
    return new this();

class Child extends Base {}


Console output:
Base creating..
Hello from Base!
Child creating..
Hello from Child!

(Tested in the nodejs v8.6.0)

Leave a Reply

Your email address will not be published. Required fields are marked *