2010-10-12 31 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