简体   繁体   English

javascript语法以从外部变量设置对象属性值

[英]javascript syntax to set object property value from external variable

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) ] }); 

the value of paths property should be assigned from external variable pathsTemp 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; }); 

Its not working.. 它不起作用..

Other options that I tried 我尝试过的其他选择

paths = pathsTemp; -- Not working -不工作
paths: paths.push(pathsTmep) -- No syntactic error but, uncaught reference error at runtime when this line is reached paths: paths.push(pathsTmep) -没有语法错误,但是到达此行时在运行时未捕获参考错误

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

Well, I got what the problem is 好吧,我明白了问题所在

  1. The correct syntax is paths: pathsTemp 正确的语法是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. The above code is syntactically correct, but the problem is it should be path instead of paths 上面的代码在语法上是正确的,但是问题在于它应该是path而不是paths

Though the sample example presented at https://developers.google.com/maps/documentation/javascript/geometry#isLocationOnEdge 尽管示例示例位于https://developers.google.com/maps/documentation/javascript/geometry#isLocationOnEdge

uses paths , it works with path for me. 使用paths ,它对我来说适用于path May be a typo in API documentation 可能是API文档中的错字

As expected, documentation bug logged 不出所料, 记录了文档错误

with reference to this stackoverflow discussion 参考此stackoverflow讨论

for the person who down voted the question : please take a look at this misleading example snippet from google documentation and change your opinion if it makes sense to you (Reputation matters a lot here, for a beginner like me) 对于不赞成该问题的人:请看一下Google文档中的这个令人误解的示例片段,如果对您有意义,请更改您的意见(在这里声望非常重要,对于像我这样的初学者而言)

在此处输入图片说明

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

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