Skip to content
Advertisement

How to get class from a constant array?

I have about twenty different classes that are segregated in an assoc array similar to this:

JavaScript

But now I want to do the array constant.

I can make it using const VAL = array(); just fine. The problem lies in making new objects using those classes

JavaScript

fails with Parse error: syntax error, unexpected '[', expecting ')' error.

I figured that I could reverse const array to variable again and it works fine:

JavaScript

but why doesn’t it work with a constant array?

I tried playing with parenthesis and constant() too. Neither new (CLASSES)["one"](), new (CLASSES["one"])(), new constant(CLASSES)["one"]() nor new constant(CLASSES["one"])() work.

Is there something I’m missing?

Advertisement

Answer

I don’t know why you would, but yes you can:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement