2011-02-04 150 views
1

我想將一個數組傳遞給一個函數並對其進行處理,但是我擔心,發生錯誤會說board_pt是未定義的。問題是什麼?這是代碼:javascript函數參數

function set_boardPoint(board_pt,turn) 
{ 
    var no = board_pt.number-1; 
    board[no].row = board_pt.row; 
    board[no].col = board_pt.col; 
    board[no].x = board_pt.x; 
    board[no].y = board_pt.y; 
    board[no].value = turn; 
    board[no].number = board_pt.number; 
} 

板是已經定義

+5

你能告訴你如何調用set_boardPoint嗎? – 2011-02-04 08:42:06

+1

我建議你專注於錯誤信息。如果board_pt是'未定義的',那麼它可能意味着它說的是什麼。 – 2011-02-04 08:44:37

回答

1

問題一個全局數組是board_pt僅具有1個項目,並且在這些情況下的js知道board_pt作爲對象:

function set_boardPoint(board_pt,turn) 
{ 
    var no = board_pt.number-1; 
    if(board[no] != undefined) 
    { 
    board[no].row = board_pt.row; 
    board[no].col = board_pt.col; 
    board[no].x = board_pt.x; 
    board[no].y = board_pt.y; 
    board[no].value = turn; 
    board[no].number = board_pt.number; 
}else 
{ 
board.row = board_pt.row; 
    board.col = board_pt.col; 
    board.x = board_pt.x; 
    board.y = board_pt.y; 
    board.value = turn; 
    board.number = board_pt.number; 
} 
} 
0

如果board_pt是一個真正的數組,那麼它不太可能具有名爲number的屬性。你的意思是length

0

從您的意見,你必須定義previous_boardPoint作爲一個數組。要在JavaScript中聲明數組,您需要在代碼中有 - var previous_boardPoint = [];。也有定義的每個元素(如果您有任何),如previous_boardPoint[0] = val1; previous_boarPoint[1] = val2;

如果這些東西是不是在你的代碼,然後在所有可能的機會,你會得到board_pt爲未定義在函數set_boardPoint