JavaScript -不能设置未定义的属性。

时间:2022-12-02 08:16:47

My code:

我的代码:

    var a = "1",
        b = "hello",
        c = { "100" : "some important data" },
        d = {};

        d[a]["greeting"] = b;
        d[a]["data"] = c;

        console.debug (d);

I get the following error:

我得到了以下错误:

Uncaught TypeError: Cannot set property 'greeting' of undefined.

未捕获的类型错误:无法设置未定义的属性“问候”。

I'm trying to do something similar to an associative array. Why isn't this working?

我试着做一些类似于关联数组的事情。为什么这不是工作吗?

5 个解决方案

#1


103  

you never set d[a] to any value.

你从不把d (a)设为任何值。

Because of this, d[a] evaluates to undefined, and you can't set properties on undefined.

正因为如此,d[a]的计算结果是未定义的,不能对未定义的属性进行设置。

If you add d[a] = {} right after d = {} things should work as expected.

如果在d ={}之后添加d[a] ={},那么就应该按照预期工作。

Alternatively, you could use an object initializer:

或者,您可以使用对象初始化器:

d[a] = {
    greetings: b,
    data: c
};

Or you could set all the properties of d in an anonymous function instance:

或者你可以在一个匿名函数实例中设置d的所有属性:

d = new function () {
    this[a] = {
        greetings: b,
        data: c
    };
};

If you're in an environment that supports ES2015 features, you can use computed property names:

如果您处于支持ES2015特性的环境中,您可以使用计算属性名称:

d = {
  [a]: {
    greetings: b,
    data: c
  }
};

#2


22  

You have to set d[a] to either an associative array, or an object:

您必须将d[a]设置为关联数组或对象:

  • d[a] = [];
  • d[]=[];
  • d[a] = {};
  • d[]= { };

Without setting, this is what's happening:

没有设定,这就是正在发生的事情:

d[a] == undefined, so you're doing undefined['greeting']=b; and by definition, undefined has no properties. Thus, the error you received.

d[a] ==未定义,所以你没有定义['greeting']=b;根据定义,undefined没有属性。因此,您收到的错误。

#3


5  

The object stored at d[a] has not been set to anything. Thus, d[a] evaluates to undefined. You can't assign a property to undefined :). You need to assign an object or array to d[a]:

存储在d[a]中的对象没有设置为任何东西。因此,d[a]的计算结果是未定义的。您不能将属性指定为未定义的:)。您需要将一个对象或数组分配给d[a]:

d[a] = [];
d[a]["greeting"] = b;

console.debug(d);

#4


4  

In javascript almost everything is an object, null and undefined are exception.

在javascript中,几乎所有东西都是对象,null和undefined都是异常。

Instances of Array is an object. so you can set property of an array, for the same reason,you can't set property of a undefined, because its NOT an object

数组的实例是一个对象。所以你可以设置一个数组的属性,因为同样的原因,你不能设置一个未定义的属性,因为它不是一个对象。

#5


0  

i'd just do a simple check to see if d[a] exists and if not initialize it...

我只做一个简单的检查,看看d[a]是否存在,如果没有初始化它……

var a = "1",
    b = "hello",
    c = { "100" : "some important data" },
    d = {};

    if (d[a] === undefined) {
        d[a] = {}
    };
    d[a]["greeting"] = b;
    d[a]["data"] = c;

    console.debug (d);

#1


103  

you never set d[a] to any value.

你从不把d (a)设为任何值。

Because of this, d[a] evaluates to undefined, and you can't set properties on undefined.

正因为如此,d[a]的计算结果是未定义的,不能对未定义的属性进行设置。

If you add d[a] = {} right after d = {} things should work as expected.

如果在d ={}之后添加d[a] ={},那么就应该按照预期工作。

Alternatively, you could use an object initializer:

或者,您可以使用对象初始化器:

d[a] = {
    greetings: b,
    data: c
};

Or you could set all the properties of d in an anonymous function instance:

或者你可以在一个匿名函数实例中设置d的所有属性:

d = new function () {
    this[a] = {
        greetings: b,
        data: c
    };
};

If you're in an environment that supports ES2015 features, you can use computed property names:

如果您处于支持ES2015特性的环境中,您可以使用计算属性名称:

d = {
  [a]: {
    greetings: b,
    data: c
  }
};

#2


22  

You have to set d[a] to either an associative array, or an object:

您必须将d[a]设置为关联数组或对象:

  • d[a] = [];
  • d[]=[];
  • d[a] = {};
  • d[]= { };

Without setting, this is what's happening:

没有设定,这就是正在发生的事情:

d[a] == undefined, so you're doing undefined['greeting']=b; and by definition, undefined has no properties. Thus, the error you received.

d[a] ==未定义,所以你没有定义['greeting']=b;根据定义,undefined没有属性。因此,您收到的错误。

#3


5  

The object stored at d[a] has not been set to anything. Thus, d[a] evaluates to undefined. You can't assign a property to undefined :). You need to assign an object or array to d[a]:

存储在d[a]中的对象没有设置为任何东西。因此,d[a]的计算结果是未定义的。您不能将属性指定为未定义的:)。您需要将一个对象或数组分配给d[a]:

d[a] = [];
d[a]["greeting"] = b;

console.debug(d);

#4


4  

In javascript almost everything is an object, null and undefined are exception.

在javascript中,几乎所有东西都是对象,null和undefined都是异常。

Instances of Array is an object. so you can set property of an array, for the same reason,you can't set property of a undefined, because its NOT an object

数组的实例是一个对象。所以你可以设置一个数组的属性,因为同样的原因,你不能设置一个未定义的属性,因为它不是一个对象。

#5


0  

i'd just do a simple check to see if d[a] exists and if not initialize it...

我只做一个简单的检查,看看d[a]是否存在,如果没有初始化它……

var a = "1",
    b = "hello",
    c = { "100" : "some important data" },
    d = {};

    if (d[a] === undefined) {
        d[a] = {}
    };
    d[a]["greeting"] = b;
    d[a]["data"] = c;

    console.debug (d);