Is it possible to call a JavaScript function using an array of values as arguments ?

Posted by Moshe Levine on Stack Overflow See other posts from Stack Overflow or by Moshe Levine
Published on 2010-04-15T11:07:36Z Indexed on 2010/04/15 11:13 UTC
Read the original article Hit count: 302

I'm looking for another way of doing the following:

function call_any_function(func, parameters){
    // func => any given function
    if(parameters.length==0){ func(); }    
    if(parameters.length==1){ func(parameters[0]); }    
    if(parameters.length==2){ func(parameters[0], parameters[1]); }    
    if(parameters.length==3){ func(parameters[0], parameters[1], parameters[2]); }    
    if(parameters.length==4){ func(parameters[0], parameters[1], parameters[2], parameters[3]); }
    // ... and so on
};

It seems basic but I couldn't find an answer.

Any ideas?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about functions