0

I have one function named root and at next part I am storing function name in variable (call) . I want to call function by using variable name For Example:-

(note:- not using root() using variable call's value )

 call = "root";

 root = function() {
    name = "This is My root function";
    console.log(name)
 } 

Can we call root function by value of variable (call) Is it possible.

  • 2
    While this is possible I strongly suggest you consider your design. Do you absolutely NEED the variable to contain the function name? Javascript variables can point to the function itself: `call = root` instead of `call = "root"`. If all you need is to implement some sort of function calling logic then use function references instead of function names. – slebetman Nov 06 '17 at 03:20

2 Answers2

2

Assuming call is a global browser variable, you can do:

window[call]();
Jim Cote
  • 1,746
  • 3
  • 15
  • 26
2

eval() is probably what you need, but use with caution.

Sean Wu
  • 31
  • 2