javascript语法以从外部变量设置对象属性值 [英] javascript syntax to set object property value from external variable

查看:142
本文介绍了javascript语法以从外部变量设置对象属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过代码片段更好地解释我的情况

I can better explain my case by code snippet

var cascadiaFault = new google.maps.Polyline({
    paths: [
      new google.maps.LatLng(49.95, -128.1),
      new google.maps.LatLng(46.26, -126.3),
      new google.maps.LatLng(40.3, -125.4)
    ]
});

paths属性的值应从外部变量pathsTemp

the value of paths property should be assigned from external variable pathsTemp

var pathsTemp = [];
for (var i = 0; i < boxes.length; i++) {
	var bounds = boxes[i];
	// Perform search over this bounds
	pathsTemp.push(bounds.getCenter());
}
var cascadiaFault = new google.maps.Polyline({
	paths: pathsTemp;
});

它不起作用.

我尝试过的其他选项

paths = pathsTemp;-不起作用
paths: paths.push(pathsTmep)-没有语法错误,但是到达此行时在运行时未捕获参考错误

paths = pathsTemp; -- Not working
paths: paths.push(pathsTmep) -- No syntactic error but, uncaught reference error at runtime when this line is reached

PS:我没有javascript背景,但是我没有时间开始从头开始阅读语法和规则(但是,我可以理解大多数已编写的js代码)

PS: I don't have javascript background and unfortunately I don't have time to start reading the syntax and rules from scratch (However, I can understand most of the js code already written)

推荐答案

好,我明白了问题所在

  1. 正确的语法是paths: pathsTemp

var pathsTemp = [];
for (var i = 0; i < boxes.length; i++) {
 	var bounds = boxes[i];
  	// Perform search over this bounds
   	pathsTemp.push(bounds.getCenter());
}
var cascadiaFault = new google.maps.Polyline({
   	paths: pathsTemp
});

  1. 上面的代码在语法上是正确的,但是问题在于它应该是path而不是paths
  1. The above code is syntactically correct, but the problem is it should be path instead of paths

尽管示例示例显示在https: //developers.google.com/maps/documentation/javascript/geometry#isLocationOnEdge

使用paths,对我来说它与path一起使用.可能是API文档中的错字

uses paths, it works with path for me. May be a typo in API documentation

如预期的那样,记录了文档错误

参考此stackoverflow讨论

with reference to this stackoverflow discussion

这篇关于javascript语法以从外部变量设置对象属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆