-1

In GSC, you are able to make a variable become the name of a function that you thread. It looks like this:

variable = "pizza";
[[variable]]();

the engine then reads it like:

pizza();

my question is, is it possible to do that in javascript as easily or do I have to make if/else/switch statements for it?

1 Answers1

0

my question is, is it possible to do that in javascript as easily or do I have to make if/else/switch statements for it?

If you want to use the safe, fail-proof way, then you can access such variables only in two contexts.

  1. If the variable is in global context, in the case of which, you can do window[variable]();
  2. Else if the variable is a property of an object, in the case of which, you can do obj_name[variable](), basically anything that can be accessed via bracket notation. window is an object too.


Then there's always the dirty way:
  • You can use highly evil eval like eval(variable + "()") or you can use the Function constructor in the same way. Note however that both the methods can be misused and are highly advised against.
Amit Joki
  • 58,320
  • 7
  • 77
  • 95