【发布时间】:2015-09-10 18:45:36
【问题描述】:
我是 javascript 的新手。面临以下问题
var list = [];
initList(); // after this function I have some elements in `list`
var node = list[0]; //after the assignment firebug says `node` is undefined. `list[0]` is defined and contains object like {a:"value",b:"value1", ...}
我做错了什么?
我试图像var node = {}; 一样声明node。但它也不起作用。
这里是我的 initList 函数。希望会有人想要掌握它:))
function initList() {
d3.json("./config.json", function (error, txt) {
if (error) {
console.debug(error)
}
txt.forEach(function (bop, i) {
var nd = {};
d3.json("./cgi-bin/getshmid.sh?key=" + (195951310 + bop.bop_id), function (error, shm) {
if (error) {
console.debug(error)
} else {
console.debug(bop, i)
}
d3.select("#tplace")
.append("h3")
.text("BOP's ID = " + bop.bop_id)
var table = d3.select("#tplace")
.append("table")
.attr("id", "shmid_" + shm.id);
var thead = table.append("tr")
thead.append("td")
.text("Блоки детектирования, ID")
thead.append("td")
.text("Текущее значение")
thead.append("td")
.text("Предупредительная уставка")
thead.append("td")
.text("Аварийная уставка")
thead.append("td")
.text("Статус")
thead.append("td")
.text("Превышение ПУ")
thead.append("td")
.text("Превышение АУ")
bop.BDs.forEach(function (bd, i) {
var tbody = table.append("tr")
.attr("class", "bd")
tbody.append("td")
.text(bd.index + 1)
tbody.append("td")
.attr("id", "val")
tbody.append("td")
.attr("id", "pz")
.attr("class", "edit " + (516 + i * 16) + "")
tbody.append("td")
.attr("id", "az")
.attr("class", "edit " + (518 + i * 16) + "")
tbody.append("td")
.attr("id", "status")
tbody.append("td")
.attr("id", "state_pz")
tbody.append("td")
.attr("id", "state_az")
})
nd = foo(shm.id);
list.push(nd);
})
})
})
};
function foo(id) {
var node = {
value: id,
table: d3.select('#shmid_' + id),
tr: d3.select('#shmid_' + id).selectAll(".bd"),
bds: []
};
node.tr.each(function (d, i) {
var bd = {};
d3.select(this).selectAll("td").each(function (d, i) {
switch (d3.select(this).attr("id")) {
case 'val':
bd.val = d3.select(this);
case 'pz':
bd.pz = d3.select(this);
case 'az':
bd.az = d3.select(this);
case 'status':
bd.status = d3.select(this);
case 'state_pz':
bd.stpz = d3.select(this);
case 'state_az':
bd.staz = d3.select(this);
}
});
node.bds.push(bd);
});
return node;
};
【问题讨论】:
-
你在你的函数中做
var list = ...吗? -
你能显示
initList函数吗? -
根据您指定的示例,您的节点结果不能未定义。
-
请保持简单:在
initList()、console.log(list)之后。不要让我们受制于充满我们无法运行的代码的屏幕和屏幕。
标签: javascript variable-assignment