-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?

7
  • 3
    There are many similar questions, for example stackoverflow.com/questions/3619046/… Commented May 23, 2015 at 14:25
  • Relevant? stackoverflow.com/questions/5905492/… Commented May 23, 2015 at 14:26
  • By "thread", you mean "call"? Commented May 23, 2015 at 14:30
  • @b2238488: Not really, that one talks about creating functions Commented May 23, 2015 at 14:32
  • 1
    @guest271314: he seems to refer to cod1.eu/script/dm.gsc Commented May 23, 2015 at 16:42

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.