简体   繁体   中英

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

 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

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)

Well, I got what the problem is

  1. The correct syntax is 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

Though the sample example presented at https://developers.google.com/maps/documentation/javascript/geometry#isLocationOnEdge

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

As expected, documentation bug logged

with reference to this stackoverflow discussion

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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