我的目標是動態設置,如姓名,隱蔽性和no_parent性能動態設置屬性,但它一直給我:不能在一個對象
TypeError: Cannot set property 'name' of undefined
即使我通過參數傳遞之前,將初始化scorcroot
。
下面是代碼:
adattamento: function(data) {
var continua = true;
var scorcfat = this.famiglia;
var scorcroot = {};
this.controlloprimi(scorcroot, scorcfat);
this.root = scorcroot;
console.log("hey!")
console.log(this.root);
},
controlloprimi: function(scorcroot, scorcfat) {
scorcroot.name = scorcfat.name;
scorcroot.hidden = false;
scorcroot.no_parent = true;
if (scorcfat.father != null) {
if (scorcfat.mother != null) {
scorcroot.children = [{}, {}, {}];
this.controlloprimi(scorcroot.children[1], scorcfat.father);
scorcroot.children[2].name = "";
scorcroot.children[2].hidden = true;
scorcroot.children[2].no_parent = false;
this.controlloprimi(scorcroot.children[3], scorcfat.mother)
} else {
scorcroot.children = [{}]
this.controlloprimi(scorcroot.children[1], scorcfat.father);
}
}
if (scorcfat.mother != null) {
scorcroot.children = [{}, {}];
this.controlloprimi(scorcroot.children[1], scorcfat.mother);
}
},
確保'scorcroot.children [2 ]'不是'undefined'。只是一個指針,JS中的索引從0開始,而不是1 – Rajesh
是的問題是數字...謝謝 – ImFireblade