正在處理需要爲數組添加點的項目。以下是創建,然後試圖將一個對象添加到陣列的代碼:JavaScript 2D數組未捕獲TypeError
var points = [];
points[0] = [];
points[0][0] = 0; //x-coord
points[0][1] = 0; //y-coord
points[points.length][0] = x; //Breaks on this line
points[points.length][1] = y;
確切的錯誤我得到的是遺漏的類型錯誤:無法設置的不確定財產「0」。每次按下按鈕時都會運行此代碼,其他值已經設置爲ints
。我認爲JavaScript允許你在數組中設置值?
我推薦使用對象的數組,而不是:'分= [];點[0] = {x:0,y:0};' – jimw