Possible Duplicate:
Javascript dynamic variable name
I’m trying to create a function in javascript that has a name dependent on a variable.
For instance:
var myFuncName = 'somethingWicked';
function myFuncName(){console.log('wicked');};
somethingWicked(); // consoles 'wicked'
I can’t seem to figure out a way to do it…. I tried to eval it, but then when I try and use the function at a later time it ‘doesnt exist’.. or more exactly I get a ReferenceError that the function is undefined…
Any help would be appreciated.
You could assign your functions to an object and reference them like this:
Alternatively, you could keep them as globals, but you would still have to assign them through the
windowobject: