比方說,我有一個對象如下:分配對象2-d陣列
function node(xVal, yVal, nodeType) {
this.xVal = xVal; // x-coordinate of node
this.yVal = yVal; // y-coordinate of node
this.nodeType = nodeType; // node type - outside the scope of this question
}
在試圖上的x由-Y虛擬平面創建一系列節點,我所指定的兩維陣列如下:
var ROWS = 3; // number of rows in the array
var COLS = 10; // number of columns in the array
var Nodes = new Array(ROWS);
for (var i=0; i < ROWS; i++) {
for (var j=0; j < COLS; j++) {
Nodes[i][j] = new node(0, 0, "Type A");
}
}
我期待嵌入for循環以上將允許我初始化3×10陣列,每個具有「節點」的對象,但一些似乎引起誤差。任何想法1)什麼可能會導致錯誤,2)如何改善邏輯將不勝感激!
節點[i]'沒有被定義。你必須先用一個值初始化它,即「第二級」數組。 –