1
class CustomTable
{
constructor(div_id, headings) {
this.div = div_id;
this.header_titles = headings;
this.item_list = new Array();
var _this = this;
this.add_item = function(items)
{
_this.item_list.push(items);
console.log(_this.item_list);
}
this.remove_item = function(item_index)
{
_this.item_list.splice(item_index, 1);
console.log(_this.item_list);
}
this.drawTable = function()
{
var t = "<table class='table' style='width:100%'>";
t += "<thead>";
t += " <tr>";
t += " <th>#</th>";
for (var i = 0; i < _this.header_titles.length; i++)
{ t += "<th>" + _this.header_titles[i] + "</th>"; }
t += " <th>Add</th>";
t += " </tr>";
t += "</thead>";
t += "<tbody>";
for (var i = 0; i < _this.item_list.length; i++)
{
t += "<tr>";
t += "<td>" + i + "</td>";
console.log(i);
var subitem_count = _this.item_list[i].length;
// ^^^^^^^^^^^^^^^^^^
// This errors out: TypeError undefined
for (var j = 0; j < subitem_count; i++)
{
t += "<td>" + _this.item_list[i][j] + "</td>"
}
t += "</tr>"
}
t += "</tbody>";
t += "</table>";
document.getElementById(_this.div).innerHTML = t;
}
}
}
var ct = new CustomTable("server_list",["Server Name","IP Address", "RAM in GB"]);
ct.add_item(["QMM-TRGEXCH01","192.168.0.225","2GB"]);
ct.add_item(["QMM-SRCEXCH01","192.168.0.226","2GB"]);
ct.add_item(["QMM-TRGAGENT01","192.168.0.227","2GB"]);
ct.add_item(["QMM-SRCAGENT01","192.168.0.228","2GB"]);
ct.add_item(["QMM-MIGCONSOLE","192.168.0.229","2GB"]);
ct.drawTable();
Please view this JSFiddle爲什麼我的javascript代碼返回TypeError undefined?
我已經找遍和想不通爲什麼要使用JavaScript保持示數出來。該變量是在範圍和我已經使用
_this.item_list [I] .constructor ===陣列
檢查它,它是一個數組。
第一次迭代時出現此錯誤。
console.log(i); // i = 0時出錯
所以它不是代碼迭代超出界限。這可能是代碼的問題,但也有其他問題。請看小提琴,我已經更新了它,並從所有for循環中刪除=,但我仍然得到相同的錯誤。
這可能是一個問題很好,但即使去掉後=我仍然得到這個錯誤。另外,當我得到這個錯誤時,它仍然是for循環中的第一次迭代。我仍然是0. – ultimal
@ultimal檢查更新後的帖子。 –
謝謝!謝謝!謝謝!就是這樣! – ultimal