以后如何声明对象并分配其属性

时间:2019-05-28 12:49:14

标签: javascript object javascript-objects

我下面有创建Google Maps Rectangle Object的示例。我只想声明对象,例如“ rectangle = new google.maps.Rectangle()”,然后访问其属性,例如“ rectangle.bounds”,但这似乎不起作用

rectangle = new google.maps.Rectangle({
    bounds: bounds,
    editable: true,
    draggable: true
});

3 个答案:

答案 0 :(得分:0)

bounds不是属性,而是构造函数的参数。 CMIIW。

根据documentation,您可以使用方法bounds获得getBounds()属性

这应该有效。我没试过 var bounds = rectangle.getBounds()

答案 1 :(得分:0)

根据docs,Rectangle类接受类型为 google.maps.RectangleOptions 的可选参数。该对象实际上包含边界,可编辑等属性。

如果您像实例化Rectangle的新实例

rectangle = new google.maps.Rectangle({
    bounds: bounds,
    editable: true,
    draggable: true
});

实际上只有您定义的三个属性存在。此外, bounds 属性将是未定义的,因为为其分配了不存在的bounds变量。

不幸的是,谷歌没有提供创建这种RectangleOptions对象的直接方法。

因此正确的方法是使用一个RectangleOptions对象可用的所有属性创建一个新的常规对象,并将其提供给Rectangle的构造函数。

var options = {
  bounds: new google.maps.LatLngBounds(),
  clickable: true,
  draggable: false,
  editable: false,
  fillColor: '#FF0000',
  fillOpacity: 1,
  map: new google.maps.Map(""),
  strokeColor: '#FF0000',
  strokeOpacity: 1,
  strokePosition: google.maps.StrokePosition.CENTER,
  strokeWeight: 1,
  visible: true,
  zIndex: 1
}
var rectangle = new google.maps.Rectangle(options);
console.log(rectangle.bounds);

答案 2 :(得分:-1)

disksize

请像上面一样尝试

相关问题