我的目标是动态设置诸如name,hidden和no_parent之类的属性,但它一直在给我:
TypeError:无法设置未定义的属性“名称”
即使我在通过参数传递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[3]
不是对象,因为您仅使用3个对象初始化了scorcroot.children
数组。因此scorcroot.children [3]是未定义的,并且您要在undefined上设置属性。