如何在静态方法中引用当前类?

时间:2019-12-19 12:20:45

标签: javascript oop

我有两个课

class AbstractPerson {
  static createFromPayload() {
    // here I need find the class that this method was called from
    // So, when I call 'Driver.createFromPayload()' I need to get a reference to Driver class here
    const ClassToUse = ...
    // get its static fields prop
    const fields = ClassToUse.fields;
    // and create a new instance of that class
    const result = new ClassToUse();
    // ... then do something with the fields
  }
}

class Driver extends Person {
    age = 0;
    firstName = '';
    lastName = '';

    static fields = ['age', 'firstName', 'lastName']
}

是否可以在AbstractPerson类的createFromPayload静态方法中获取对Driver类的引用?

get(...).then(data => Driver.createFromPayload(data))

0 个答案:

没有答案