我想将数组传递给函数并对其进行处理,但我担心会发生错误,说board_pt未定义。问题是什么?这是代码:
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;
}
board是已定义的全局数组
答案 0 :(得分: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;
}
}
答案 1 :(得分:0)
如果board_pt
是一个真正的数组,那么它不太可能有一个名为number
的属性。你的意思是length
?
答案 2 :(得分:0)
根据您的评论,您必须将 previous_boardPoint 定义为数组。要在javascript中声明数组,您需要在代码中使用 - var previous_boardPoint = [];
。还要定义每个元素(如果有的话),如previous_boardPoint[0] = val1; previous_boarPoint[1] = val2;
等等。
如果您的代码中没有这些内容,那么在所有可能的情况下,您将在函数 set_boardPoint 中将 board_pt 定义为未定义。