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 
*/
2
  • this.constructor.a should work for dynamic static variable resolution. Commented Mar 8, 2021 at 9: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. Commented Mar 8, 2021 at 15:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.