2015-10-23 228 views
0

我想將字符串變量和兩個數組傳遞給全局函數。事情是這樣的:JavaScript將數組作爲參數傳遞給具有多個參數的函數

function send_times_to_device(stop_name, times, headsigns) { 
    // function code here 
} 

在後面的代碼:

... 
var stop_name = "temp"; 
var times = new Array(json.length); 
var headsigns = new Array(json.length); 
... 
if(times.length < 6){ 
    send_times_to_device(stop_name, times, headsigns); 
} 
... 

我怎麼會在Javascript中正確地做到這一點?

在此先感謝!

編輯: 你們是對的,我的代碼在其他地方出現錯誤,這個工程!

+1

有錯誤嗎? – MinusFour

+1

我沒有看到問題 –

+0

,看起來技術上是正確的 – Jesse

回答

1

new Array(n),創建一個數組,其中包含n個未定義的條目。

如果你想用一個整數值作爲第一個條目使用創建磁盤陣列:

function send_times_to_device(stop_name, times, headsigns) { 
    // function code here 
    console.log(stop_name); 
    console.log(times); 
    console.log(headsigns); 
} 

var stop_name = "temp"; 
var times = [json.length]; 
var headsigns = [json.length]; 

send_times_to_device(stop_name, times, headsigns); 
相關問題