views:

50

answers:

1

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?

+5  A: 

Oh, yes:

func.apply({}, parameters)

the first parameter is what you want this to be inside the function.

Victor
Stupid question, simple answer. Thanks!
Moshe Levine
There are no stupid questions, only insanely cool javascript features :)
Victor