2

Having the function name 'foo' and its argument 'arg' in strings, is there a way to execute foo(arg)? Do I need to use eval? I want to do something like send(foo, arg) if it were in Ruby.

1 Answer 1

4

You can use indexer notation:

window[name](arg);
Sign up to request clarification or add additional context in comments.

2 Comments

I agree on window[name] but arg was also a string. window[name](window[arg]); feels as not what you always want. this[name](this[arg]); might work but not for local vars.
Assuming the function is in the global scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.