這個代碼是創建一個表組織元素與DOM的表 - 的JavaScript
var table = document.createElement("table");
if ((lines > 0) && (columns> 0)) {
var tbody = document.createElement("tbody");
var tr, td;
for (var i = 0; i < lines; i++) {
tr = document.createElement("tr");
for (var j = 0; j < columns; j++) {
td = document.createElement("td");
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
document.body.appendChild(table);
這樣圖像1
class Panel
和function
到putAndShowEquipment
function Panel(id, line, column) {
this.id = id;
this.lines = line;
this.columns = column;
this.equipments = [];
for(var i = 0; i < lines; i++{
this.equipments[i] = [];
}
}
Panel.prototype.putAndShowEquipment = function(line, column, equipment) {
if ((line >= 0) && (line < this.lines)
&& (column >= 0) && (column < this.columns)
&& ((equipament === void 0) || (equipament instanceof Equipament))) {
this.equipaments[line][column] = equipament;
if (equipament) {
equipament.show(this.cell(line, column));
} else {
(new View()).show(this.cell(line, column));
}
}
return this;
}
,我想成爲這個形象2
,我可以做,如果我做
//panel with 4 lines and 4 columns to 16 equipments
(new Panel("panel",4,4))
.putAndShowEquipment (0, 0, new Equipament())
.putAndShowEquipment (0, 1, new Equipament())
.putAndShowEquipment (1, 0, new Equipament())
.putAndShowEquipment (1, 1, new Equipament())
.putAndShowEquipment (2, 0, new Equipament())
.putAndShowEquipment (2, 1, new Equipament())
...
;
但我儘量爲四個何時創建lines
和columns
使用for
,把equipaments,我想也許有限制列,換另一行,我已經準備好了,現在只是一個的問題計算
//four lines
for (var i = 0; i < lines; i++) {
//four columns
for (var j = 0; j < columns; j++) {
//if column is four
if (j === 4) {
//change line
//this.equipaments.lenght = 16 equipments
panel.putAndShowEquipment(i + 1, j, this.equipaments[i]);
} else {
panel.putAndShowEquipment(i, j, this.equipaments[i]);
}
}
}
有什麼建議嗎?
有點難理解,但是如果有4列,那麼'j'在循環內永遠不會等於'4',因爲循環已經停止。不知道爲什麼你需要'if'語句,但是我們不知道'this'的值在你的最後一個代碼塊中。看起來應該只是'panel.putAndShowEquipment(i,j,/ * ???? * /)',但沒有足夠的信息來知道第三個參數應該從哪裏來。 – 2016-05-14 15:13:25
另外,在'putAndShowEquipment'方法中,似乎'(line
2016-05-14 15:14:12
@squint,謝謝,我看到了錯誤,我編輯了'(line