简体   繁体   English

JavaScript原型问题(使用Google Maps)

[英]Javascript prototype problems (with google maps)

I'm trying to write a clean GoogleMap.js script. 我正在尝试编写干净的GoogleMap.js脚本。 I have created a js class wich contains gMarker and gInfoWindow and i want to set a "openedInfoWindow" property in its prototype (shared), so i can close it and change it anytime the user clicks on the particular gMarker without stating it as global. 我创建了一个包含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;
}

The "alert" is for debug purpose and, everytime i click it, it shows me the content of the InfoWindow "linked" to the gMarker i just clicked on. “警报”用于调试目的,每次单击它时,都会向我显示“链接”到我刚刚单击的gMarker的InfoWindow的内容。 So the "openedInfoWindow" doesn't work how i hope. 因此,“ openedInfoWindow”无法正常运行。 Can anyone help me? 谁能帮我?

PS. PS。 Here's the function that i use to create the gMarkerWInfo inside the "GoogleMap" class: 这是我用来在“ 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;
}

Reading this article i found this out: " The definition: A function's prototype property is the object that will be assigned as the prototype to all instances created. [...] It's important to understand that a function's prototype property has nothing to do with its actual prototype. " 阅读本文时,我发现:“ 定义:函数的prototype属性是将作为原型分配给所有创建的实例的对象。[...]重要的是要了解函数的prototype属性与它的实际原型。

So i understood that the Object.prototype , wich i was trying to use before, isn't the same as the Function.prototype , wich suits my interests. 因此,我了解到我之前尝试使用的Object.prototype与符合我的兴趣的Function.prototype

So here it is, finally working: 就是这样,终于可以了:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM