0

Struggling a bit with inheritance of static properties. My Main class has a static property a. I would like for my Sub class to override that property. Also, I can only access 'a' in the constructor using Main.a. I would like that new Sub() uses Sub.a in its constructor. Can I do this, if so, how?

class Main {

    static a = "Is Main";
  
  constructor() {
    
    console.log( "Constructor Main " + Main.a)  // "Is Main"
    console.log( "Constructor This " + this.a)  // undefined
  }
  
}
class Sub extends Main {
    static a = "Is Sub"
}

var sub = new Sub();
console.log( sub.a );       // undefined

 /*
Constructor Main Is Main
Constructor This undefined
undefined 
*/
Kim Aldis
  • 583
  • 1
  • 4
  • 15
  • `this.constructor.a` should work for dynamic static variable resolution. – deceze Mar 08 '21 at 09:16
  • Got me in the right direction. 'this.constructor.a' works to get at 'sub.a' everywhere except in a static method where you have to use 'this'. Thanks for the start. – Kim Aldis Mar 08 '21 at 15:20

0 Answers0