qqruza
qqruza

Reputation: 1417

Variable as a function

I cannot achieve a task of passing a variable into a function.

So first line of code is:

window.action1(news);

After it has been executed I would like use "news" as a function:

window.action1 = function (action2) {
  window.action2(); // which supposed to be news instead of action2
}

Upvotes: 0

Views: 49

Answers (1)

Matteo Tassinari
Matteo Tassinari

Reputation: 18584

If you pass a function name as a string, you would be calling:

window[action2](); // <-- notice the use of square bracket notation, since "action2" is just a variable string

if instead you are passing the function directly, just call it:

action2();

Upvotes: 2

Related Questions