1

I have something like this:

var functions = ['function1','function2','function3'];

How would I run a certain function?

function[0];

Ideally I would like to use a count of some sort to call a function individually. I am able to get the correct output but it does not actually execute the function.

Thanks!

4
  • 2
    You are storing strings (function names) in your array, not functions. Are these functions defined in the global scope? Are they methods exposed by a specific object? Commented May 27, 2014 at 8:05
  • Well, I changed the from function names to functions using this as an example: jsfiddle.net/mzxCk. It however only appears to work in firefox and not chrome (the browser I am testing in). Commented May 27, 2014 at 8:29
  • Your fiddle works fine for me on Chrome 35.0.1916.114 m. Commented May 27, 2014 at 8:32
  • And for me. It works in firfox, i at least have something to work with. Thanks for all the help . Your time is appreciated! Commented May 27, 2014 at 8:36

3 Answers 3

2
var array_of_functions = [
    first,
    second,
    third,
    forth
]

and call like

array_of_functions[count](parameters);

example

array_of_functions[0]();

or

array_of_functions[0]('string');

JSFIDDLE DEMO: http://jsfiddle.net/mzxCk/

Sign up to request clarification or add additional context in comments.

2 Comments

You should note that first, second etc, obviously need to be defined as functions first (e.g. function first() { //do something }
They are defined, but still does not execute. Does the placement of the function matter?
1

Like this:

window[functions[0]]();
               //^---------- use index here

Use bracket notation to get the function name and call it by ().

This answer assumes that you have those functions with those names in global scope.

Fiddle

2 Comments

Thats what I thought but it does not seem to work. If I output that in an alert, it shows the function name as expected, but it does not execute as expected.
@user3291686, check out my demo
0

You also need to consider calling scope, which is why you should be using call or apply depending on whether you're passing your arguments as separate objects or an array see here for a description of both.

If your functions contains a collection of strings, you'll need to invoke them (with args) like this (note the this as the first param of call, this is the scope the function will run under, so in this case window):

var func1 = function (msg) {
  document.write("func1 called with message - " + msg + ".");
};
var functions = ['func1', 'func2'];
window[functions[0]].call(this, 'here is a message');

If functions contains a collection of Function then you need to call them like this:

var functions = [
  function (msg) {
    document.write("func1 called with message - " + msg + ".");
  },
  function (msg) {
    document.write("func2 called with message - " + msg + ".");
  }];
functions[0].call(this, 'here is a message');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.