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.

Possible Duplicate:
Calling dynamic function with dynamic parameters in Javascript

I have an array representing the parameters I need to pass to a function call. How can I construct this function call dynamically?

e.g.

function constructRequest(params) {
   //params is an array of params to be sent to myFunction()

   myFunction(params[0],params[1], ... , params[i]);

   //myFunction() needs to have the array deconstructed and is ready 
   // to accept optional params
}

Thanks

share|improve this question
 
The tl;dr version of the other question is to use myFunction.apply(window, params). –  Ben Blank Oct 12 '10 at 17:44
 
This is not a duplicate of the other question. The other question is asking about passing functions. –  Kyralessa Mar 20 at 16:49
add comment

marked as duplicate by VoteyDisciple, Sachin Shanbhag, BrunoLM, Vivin Paliath, Ben Blank Oct 12 '10 at 17:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

Use arguments:

function A()
{
    alert( arguments[0] ); // 1
    alert( arguments[1] ); // 2
    alert( arguments[2] ); // 3
}

A(1, 2, 3);

More info on MDC (Mozilla Develop Center).

share|improve this answer
 
You've got this the other way around. bba has an array already and needs to break it down in order to call another function. –  VoteyDisciple Oct 12 '10 at 17:15
 
I dont think this answers my question..maybe I'm wrong - could you put it in terms of my example code? –  bba Oct 12 '10 at 17:15
 
@bba: That's what I understand from your question. Provide more info then. –  BrunoLM Oct 12 '10 at 17:24
add comment

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