我正在查看 Google Maps API MVC 使用示例。请参阅https://developers.google.com/maps/articles/mvcfun?csw=1
在第一个简单示例中,我无法理解marker.bindTo() 调用。 bindTo() 实际上是 MVC 对象的一个方法(参见https://developers.google.com/maps/documentation/javascript/reference#MVCObject)。标记本身不是 MVC 对象,而是以 MVC 对象作为其原型(prototype)的对象的属性。那么这个bindTo方法是如何关联为marker的属性的呢?
可能是我在这里缺少的一些基本内容!
感谢您的解释。
/**
* A distance widget that will display a circle that can be resized and will
* provide the radius in km.
*
* @param {google.maps.Map} map The map on which to attach the distance widget.
*
* @constructor
*/
function DistanceWidget(map) {
this.set('map', map);
this.set('position', map.getCenter());
var marker = new google.maps.Marker({
draggable: true,
title: 'Move me!'
});
// Bind the marker map property to the DistanceWidget map property
marker.bindTo('map', this);
// Bind the marker position property to the DistanceWidget position
// property
marker.bindTo('position', this);
}
DistanceWidget.prototype = new google.maps.MVCObject();
最佳答案
描述可以在 documentation of MVCObject 找到:
MVCObject 构造函数保证是一个空函数,因此您只需编写 MySubclass.prototype = new google.maps.MVCObject(); 即可从 MVCObject 继承
此技术还将用于 google.maps.Marker
实例。
google.maps.Marker
实例的构造函数是 google.maps.MVCObject
实例的构造函数,因此标记将具有 MVCObject 的方法
因此,google.maps.Marker
的实例基本上是一个使用 google.maps.Marker
原型(prototype)的属性/方法进行扩展的 MVCObject
关于javascript - Google Maps API MVC 示例说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23082003/