1

There are two or more classes, each with a static class variable of the same name. Access to the contents of the static members usually takes place by specifying the class name.

class red {
  static color = "ff0000";
  …
}

class blue {
  static color = "0000ff";
  …
}

console.log(blue.color);

I know I can access the static members name like this

console.log(blue["color"]);

Is it possible to replace the class name by another variable?

…
let myClass = "blue";
console.log(myClass.color); ???
Denis Giffeler
  • 1,499
  • 12
  • 21

1 Answers1

0

You could always use a variable as a pointer referencing the the original class:

class blue {
  static color = "0000ff";
}

let myClass = blue;

console.log(myClass.color);
PotatoParser
  • 1,008
  • 7
  • 19
  • Thanks for the answer. However, that only shifts the problem one level, doesn't it? With ten classes, case distinctions would still have to be used for each individual class name. "Assembling" the class name as a string is what I want. – Denis Giffeler Aug 21 '20 at 09:48