Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to convert an array into a function argument sequence? Example:

run({ "render": [ 10, 20, 200, 200 ] });

function run(calls) {
  var app = .... // app is retrieved from storage
  for (func in calls) {
    // What should happen in the next line?
    var args = ....(calls[func]);
    app[func](args);  // This is equivalent to app.render(10, 20, 200, 200);
  }
}
share|improve this question
    
Check for a similar question also here: stackoverflow.com/questions/2856059/… –  Wilt Feb 25 at 11:14

4 Answers 4

up vote 83 down vote accepted

Yes. You'll want to use the .apply() method. For example:

app[func].apply(this||window,args);

EDIT - Actually, my ||window bit is superfluous, since this resolves to window when called outside of a function anyway.

share|improve this answer
10  
This is straight up G. –  Alan Jan 28 '13 at 23:40
app[func].apply(this, args);
share|improve this answer

You might want to take a look at a similar question posted on Stack Overflow. It uses the .apply() method to accomplish this.

share|improve this answer

And another post on similar topic

And here another example that I personally liked for it's readability

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.