0

Is there a way to push a list of functions into an associative array? Here's what I'm trying to do:

var inits = {
    "first"  : fn1(),
    "second" : fn2(),
    "third"  : fn3(),
    "fourth" : fn4()

};

var functions = ['first','fourth'];

for(var index in functions ) {

    inits[functions[index]];

}

The important part here is to be able to name each function and have it called based on a given array of function names. I can do this in PHP but can't figure it out how to do it in javascript.

5
  • Keep in mind that an associative array is actually just an object in Javascript. Commented Jun 18, 2014 at 19:09
  • @Mr.Alien Why would you say that? Commented Jun 18, 2014 at 19:10
  • @KodleeYin I realized I was missing the invoking part... Commented Jun 18, 2014 at 19:10
  • @Dimitri As pointed out in a comment in one of the answers, make sure when the "associative array" is defined, that the values of the elements are function definitions and not function calls! Commented Jun 18, 2014 at 19:11
  • @alexn I would chain the functions instead of storing them in an object Commented Jun 18, 2014 at 19:56

2 Answers 2

2

First, you're not actually storing functions. You're storing the return value of functions.

To store the function, you'd use

var inits = {
  'first': fn1,
  'second': fn2,
   ...
};

Secondly, you're iterating incorrectly over the array.

Use forEach:

functions = ['first', 'fourth'];

functions.forEach(function (fn) {
    inits[fn];
});

Thirdly, in your loop, you don't actually try to invoke the function. If that's your intent, you're missing a ():

functions.forEach(function (fn) {
    inits[fn]();
});
Sign up to request clarification or add additional context in comments.

1 Comment

@Dimitri It's not, though you could just as easily store the functions directly: functions [fn1, fn2]. The person who told you this is "absurd" had no business doing so, there is too little context for him to have drawn such a conclusion.
1

Just a few small changes should make it work:

var inits = {
    "first"  : function() { /* make sure there's a function body */ },

    "second" : fn2,   // or this, if you intend to call a function named `fn2`
                      // or assigned to a locally accessible fn2 variable.

    "third"  : fn3(), // or this, if fn3() returns a function ...
    "fourth" : fn4(), // but, this version *probably* isn't what you want.

};

var functions = ['first','fourth'];

// generally speaking, don't use for-in on an array.
for(var i = 0; i < functions.length; i++) {

    // use parentheses to invoke the function
    inits[functions[i]]();

}

5 Comments

That will only work if fn1, fn2 etc. returns functions.
You probably mean function() { .. } instead of fn1() { .. }.
@alexn Doesn't look like there is a downvoter ... Just someone rapidly up-voting and un-voting ...
@svidgen Thank you, just a major oversight on my part; this makes perfect sense! Why would something like this be considered bad programming?
@Dimitri Not sure. I think we're missing too much context to say what you're doing is categorically absurd or bad ...

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.