简体   繁体   English

javascript object 实例化以包括一个数组作为属性之一

[英]javascript object instantiation to include an array as one of the properties

I am trying to create an object which includes an array stPoints as one of its properties.我正在尝试创建一个 object,其中包含一个数组 stPoints 作为其属性之一。 I am getting an error message saying that stPoints is undefined.我收到一条错误消息,指出 stPoints 未定义。 What is the proper way to declare an array property within an object?在 object 中声明数组属性的正确方法是什么?

this is my code:这是我的代码:

var temp={stName:"#states.stateid#",stPoints:[]};

This is correct.这是对的。 You must be referencing it incorrectly.您必须错误地引用它。

temp.stPoints

var temp={ 'stName':"#states.stateid#", 'stPoints':[] };

However, if the property name is a valid javascript identifier, as in this case, you should not have problem.但是,如果属性名称是有效的 javascript 标识符,在这种情况下,您应该没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM