Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在对象中动态设置属性_Javascript_Object_Properties_Undefined - Fatal编程技术网

Javascript 无法在对象中动态设置属性

Javascript 无法在对象中动态设置属性,javascript,object,properties,undefined,Javascript,Object,Properties,Undefined,我的目标是动态设置诸如name、hidden和no_parent之类的属性,但它始终提供: TypeError:无法设置未定义的属性“name” 即使我在通过参数传递它之前初始化了scorcroot 代码如下: adattamento: function(data) { var continua = true; var scorcfat = this.famiglia; var scorcroot = {}; this.controlloprimi(scorcro

我的目标是动态设置诸如name、hidden和no_parent之类的属性,但它始终提供:

TypeError:无法设置未定义的属性“name”

即使我在通过参数传递它之前初始化了
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]是未定义的,您正在对未定义的属性进行设置。

scorcroot.children[3]
不是一个对象,因为您仅用3个对象初始化了
scorcroot.children
数组。所以scorcroot.children[3]是未定义的,您正在将属性设置为未定义的。

您的问题似乎是这一行

this.controlloprimi(scorcroot.children[3],scorcfat.mother)
因为您在此数组初始化中只提供了3项

scorcroot.children=[{},{},{}];
这意味着
scorcroot.children[3]
未定义的

我不确定这段代码的用途,所以我建议将其设置为4项而不是3项

scorcroot.children=[{},{},{},{}];

你的问题似乎是这条线

this.controlloprimi(scorcroot.children[3],scorcfat.mother)
因为您在此数组初始化中只提供了3项

scorcroot.children=[{},{},{}];
这意味着
scorcroot.children[3]
未定义的

我不确定这段代码的用途,所以我建议将其设置为4项而不是3项

scorcroot.children=[{},{},{},{}];

问题是数组的枚举错误。。
因此,我创建了一个包含3个对象的数组,通过调用数组[3],我调用了第4个未定义的对象,问题在于数组的枚举错误。。
因此,我创建了一个包含3个对象的数组,通过调用数组[3],我调用了第4个未定义的对象

确保
scorcroot.children[2]
不是
未定义的
。只是一个指针,JS中的索引从0开始,而不是1是的问题是数字…谢谢确保
scorcroot.children[2]
不是
未定义的
。只是一个指针,JS中的索引从0开始,而不是1是的,问题是数字…谢谢你非常感谢@ImFireblade请让我知道这对你是否有效,如果有效请接受答案谢谢你非常感谢@ImFireblade请让我知道这对你是否有效,如果有效请接受答案