Inheriting static constants in JavaScript classes

Viewed 36

How can I reference constants within an inherited class so that -

  1. They use the "local" constant if one is defined within the current class

  2. They fallback on to the parent's constant definition

As an example, I'm wondering what I would fill each instance of ???? with below to achieve the expected printed result

class Parent {
  static MY_CONST = 'aaa';

  print() {
    /* Expected result: Prints `aaa` */
    console.log('????');
  }
}


class ChildOne extends Parent {
  static MY_CONST = 'bbb';

  print() {
    /* Expected result: Prints `bbb` */
    console.log('????');
  }
}


class ChildTwo extends Parent {
  print() {
    /* Expected result: Prints `aaa` */
    console.log('????');
  }
}
0 Answers
Related