简体   繁体   中英

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.

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 .

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