[英]Javascript prototype problems (with google maps)
我正在尝试编写干净的GoogleMap.js脚本。 我创建了一个包含gMarker和gInfoWindow的js类,我想在其原型(共享)中设置一个“ openedInfoWindow”属性,因此我可以在用户单击特定gMarker时关闭它并对其进行更改,而无需将其声明为全局。
function gMarkerWInfo(gMarker,gInfoWindow){
if(!gMarker || !gInfoWindow)
return null;
this.Marker = gMarker;
this.InfoWindow = gInfoWindow;
}
gMarkerWInfo.prototype.openedInfoWindow = null;
gMarkerWInfo.prototype.openInfoWindow = function(){
if(this.openedInfoWindow){
alert(this.openedInfoWindow.getContent());
//openedInfoWindow.close();
}
this.InfoWindow.open(this.Marker.getMap(),this.Marker);
this.openedInfoWindow = this.InfoWindow;
}
“警报”用于调试目的,每次单击它时,都会向我显示“链接”到我刚刚单击的gMarker的InfoWindow的内容。 因此,“ openedInfoWindow”无法正常运行。 谁能帮我?
PS。 这是我用来在“ GoogleMap”类中创建gMarkerWInfo的函数:
this.createMarkerWInfo = function(LatLng,Name,HTML_Infos){
var gMarker = new google.maps.Marker({ position: LatLng,
animation: this.MarkerAnimation,
map: priv_Map,
title: Name
});
var gInfoWindow = new google.maps.InfoWindow({content:HTML_Infos});
var gMarkerWInfoWindow = new gMarkerWInfo(gMarker,gInfoWindow);
google.maps.event.addListener(gMarker,'click', function() {gMarkerWInfoWindow.openInfoWindow();});
return gMarkerWInfoWindow;
}
阅读本文时,我发现:“ 定义:函数的prototype属性是将作为原型分配给所有创建的实例的对象。[...]重要的是要了解函数的prototype属性与它的实际原型。 ”
因此,我了解到我之前尝试使用的Object.prototype
与符合我的兴趣的Function.prototype
。
就是这样,终于可以了:
function gMarkerWInfo(gMarker,gInfoWindow){...}
gMarkerWInfo.prototype.openedInfoWindow = null;
gMarkerWInfo.prototype.openInfoWindow = function(){
if(gMarkerWInfo.openedInfoWindow)
gMarkerWInfo.openedInfoWindow.close();
this.InfoWindow.open(this.Marker.getMap(),this.Marker);
gMarkerWInfo.openedInfoWindow = this.InfoWindow;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.