1

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?

Community
  • 1
  • 1
ACynicalGeek
  • 79
  • 3
  • 8

1 Answers1

5

You could do a few things:

//if the function is in the global scope:

window["myFunction"](); 
//or
var functionName = "myFunction";
window[functionName]();

Or maintain your own map:

var functions = {
    myFunction: function() {
        ...
    },
    ...
    myOtherFunction: function() {
        ...
    }
};

Then either of the following will work:

functions.myFunction(); //will work
functions["myFunction"](); //will also work;

var functionName = "myFunction";
functions[functionName](); //also works

Of course, you can use eval:

eval(functionName + "()");

But eval is dangerous and I don't recommend it.

Based on your fiddle

Your right and left functions have been defined in the local scope of an anonymous function. This means that they are not part of the global window object. You should use the second method I described above (constructing your own map):

var directions = {
    right: function() {
        ...
    },
    left: function() {
        ...
    }
    ...
};

var direction = "right";
directions[direction]();
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • I trie this but I got the following error; 'Uncaught TypeError: Object [object Window] has no method "direction"' – ACynicalGeek Sep 19 '12 at 23:03
  • Do you have a function called direction? – Vivin Paliath Sep 19 '12 at 23:03
  • I have a function called right. The variable called direction has a value of right. I want to run my function. – ACynicalGeek Sep 19 '12 at 23:10
  • 1
    You will need to use `window[direction]();`. Notice the lack of quotes :) – Vivin Paliath Sep 19 '12 at 23:11
  • Hmm I did this but then got the error: Uncaught TypeError: Property 'left' of object [object Window] is not a function. Maybe something to do with the structure of my functions, here's a jsfiddle: [link](http://jsfiddle.net/CMGmj/) EDIT: See original fiddle not /1! – ACynicalGeek Sep 19 '12 at 23:14
  • 1
    your `direction` var is set to `"left"`. If you have `var myFuncs = { right : function () {}, left : function () {} };` and `var direction = "right";` then `myFuncs[direction]();` will work. If `right` or `left` isn't a function, and you call it, then you're going to get an error. – Norguard Sep 19 '12 at 23:20
  • 1
    @ACynicalGeek Check out my edit. Basically your functions have been defined in the local scope of an anonymous function and not in the global scope. – Vivin Paliath Sep 19 '12 at 23:26
  • @VivinPaliath thank you for all your help. Following your advice I'm getting closer. I've now got to the point where I'm re-structuring the entire code. I have a template for a jQuery plugin and I think I'm probably better doing it that way as opposed to this horrible inline scripting I'm doing. I haven't figured it out yet, but you've really helped me out! – ACynicalGeek Sep 19 '12 at 23:47
  • @VivinPaliath I got it to work! Thank you so much. I've included the jsFiddle so you can see :) [Working Fiddle](http://jsfiddle.net/CMGmj/3/) – ACynicalGeek Sep 20 '12 at 00:02
  • @ACynicalGeek Good to hear! :) – Vivin Paliath Sep 20 '12 at 16:14