这个代码是创建一个表组织元素与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