A
A
alex4answ2020-11-13 20:39:34
typescript
alex4answ, 2020-11-13 20:39:34

Late static binding, how to use overridden property in parent class?

I'm making a library, I need something like late static linking, I don't quite understand how to do it in ts.

There is a class, it has static fields/methods, it will be inherited, and its methods should work with static properties/heir methods, how?
typescript

class A {
  static text: string = 'A class';

  static getA() {
    return A.text; // что-то типа static.text
  }
}

class B extends A {
  static text: string = 'B class';
}

console.log(B.getA()); // A class, нужно B class


I understand that there is no late binding stat in js/ts and so on and so forth.
BUT how to solve such problems?

I don't want to override these common methods in each derived class.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MagnusDidNotBetray, 2020-11-13
@alex4answ

It's incredibly difficult, but still possible.

class A {
  static text: string = 'A class';

  static getA() {
    return this.text; // что-то типа static.text
  }
}

class B extends A {
  static text: string = 'B class';
}

console.log(A.getA()); // A class
console.log(B.getA()); // B class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question