javascript syntax to set object property value from external variable

三世轮回 提交于 2020-01-07 06:19:26

问题


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)


回答1:


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




回答2:


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)



来源:https://stackoverflow.com/questions/31156657/javascript-syntax-to-set-object-property-value-from-external-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!