【问题标题】:javascript object instantiation to include an array as one of the propertiesjavascript 对象实例化以包含一个数组作为属性之一
【发布时间】:2011-07-11 16:27:57
【问题描述】:

我正在尝试创建一个对象,其中包含一个数组 stPoints 作为其属性之一。 我收到一条错误消息,指出 stPoints 未定义。在对象中声明数组属性的正确方法是什么?

这是我的代码:

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

【问题讨论】:

  • 您提供的代码没问题。向我们展示认为stPoints 未定义的代码。
  • 这里是剩下的代码 var stateObj=[];变种名称; var stPoints; //var temp= Object.create{{stName:#states.stateid#},{ stPoints:[]}}; var temp={stName:"#states.stateid#",stPoints:[]}; var 坐标 = new google.maps.LatLng(#states.latitude#, #states.longitude#); stPoints.push(坐标); stateObj.push(temp);

标签: javascript arrays object


【解决方案1】:

这是正确的。你一定是引用不正确。

temp.stPoints

【讨论】:

    【解决方案2】:

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

    但是,如果属性名称是有效的 javascript 标识符,在这种情况下,您应该没有问题。

    【讨论】:

      猜你喜欢
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 2021-08-18
      • 2020-12-27
      • 2013-09-10
      • 1970-01-01
      • 2015-09-20
      相关资源
      最近更新 更多