2

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?

1 Answer 1

6

Oh, yes:

func.apply({}, parameters)

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

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

2 Comments

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

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.