Javascript原型问题(使用谷歌地图)
我正在尝试编写一个干净的GoogleMap.js脚本。 我已经创建了一个包含gMarker和gInfoWindow的js类,我想在它的原型(共享)中设置一个“openedInfoWindow”属性,这样我就可以在用户点击特定gMarker时关闭并更改它,而不必将其声明为全局Javascript原型问题(使用谷歌地图),javascript,google-maps,prototype,Javascript,Google Maps,Prototype,我正在尝试编写一个干净的GoogleMap.js脚本。 我已经创建了一个包含gMarker和gInfoWindow的js类,我想在它的原型(共享)中设置一个“openedInfoWindow”属性,这样我就可以在用户点击特定gMarker时关闭并更改它,而不必将其声明为全局 function gMarkerWInfo(gMarker,gInfoWindow){ if(!gMarker || !gInfoWindow) return null; this.Marke
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“链接”的信息窗口的内容。所以“开放式窗口”并不像我希望的那样有效。
有人能帮我吗
另外,下面是我用来在“GoogleMap”类中创建gMarkerWInfo的函数:
通过阅读我发现了这一点:“定义:函数的prototype属性是将作为原型分配给所创建的所有实例的对象。[…]理解函数的prototype属性与其实际原型无关非常重要。”
所以我知道我以前尝试使用的对象.prototype
,与函数不同.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;
}
openedInfoWindow/InfoWindow
-属性不在任何对象之间共享,它们已分配给gMarkerWInfo
-实例,每个创建的标记都有自己的属性。您可以将这些属性分配给map实例,以便能够跨标记共享它们。我甚至可以声明一个全局变量并通过这种方式实现,但我想了解prototype属性是如何工作的。如果没有原型,我甚至无法使用gMarkerWInfo.prototype.openInfoWindow=function(){…}
,这实际上是可行的。所以我在问,我怎样才能把它用于我的目的呢?我发现了,下面是答案
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;
}