2010-10-12 64 views
1

可能重复:
Calling dynamic function with dynamic parameters in JavascriptJavaScript数组参数列表

我具有表示我需要传递给函数调用的参数的阵列。我怎样才能动态地构造这个函数调用?

例如

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 
} 

感谢

+0

其他问题的tl; dr版本是使用'myFunction.apply(window,params)'。 – 2010-10-12 17:44:03

+0

这不是其他问题的重复。另一个问题是询问关于传递函数。 – 2013-03-20 16:49:53

回答

4

使用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)

+1

你有其他的方法。 bba已经有一个数组,并且需要将其分解以调用另一个函数。 – VoteyDisciple 2010-10-12 17:15:33

+0

我不认为这回答我的问题..可能我错了 - 你能把它放在我的示例代码? – bba 2010-10-12 17:15:59

+0

@bba:这就是我从你的问题中了解到的。然后提供更多信息。 – BrunoLM 2010-10-12 17:24:25