prime
prime

Reputation: 2074

Create new instance of this using ES6

Is it possible to create a new instance of self/this using ES6 inside a static method? For example;

class myClass {

  static model() {

    return new this;
  }
}

Is there a recognised pattern for this type of scenario?

Many thanks.

Upvotes: 0

Views: 174

Answers (1)

Bergi
Bergi

Reputation: 664548

Yes, this is exactly how you'd do it.

If you don't want subclasses to use the subclass constructors, refer to your class by name explicitly, similar to accessing other static methods.

Upvotes: 4

Related Questions