Possible Duplicate:
Calling a JavaScript function named in a variable
I want to use the value of a variable as the call to initiate a function, but I’m not sure if it is possible?
say I have:
function myFunction(){
// Do something
}
var functionName = "myFunction";
functionName(); // This should run the function named myFunction
Is this something that can be done?
You could do a few things:
Or maintain your own map:
Then either of the following will work:
Of course, you can use
eval:But
evalis dangerous and I don’t recommend it.Based on your fiddle
Your
rightandleftfunctions have been defined in the local scope of an anonymous function. This means that they are not part of the globalwindowobject. You should use the second method I described above (constructing your own map):