繁体   English   中英

在Eventlistener中访问原型成员

[英]Acces prototype member in an Eventlistener

在事件监听器中如何添加对象的成员? 这是一个例子,我认为这使它更容易理解;)

MapProvider = function(mapID, autocompleteID) {
  // some other members
  MapProvider.prototype.map = null;
  MapProvider.prototype.autocomplete = null;

  // init function
  MapProvider.prototype.initAutocomplete = function() {  
   // some other stuff and now i create an autocompleteObj
   this.autocomplete = new google.maps.places.Autocomplete(this.inputField, myOptions);
   this.autocomplete.bindTo('bounds', this.map);

   // until now everything went fine
   // now i want to listen to the autocompleteObj
   // the handler is working fine aswell
   google.maps.event.addListener(this.autocomplete, 'place_changed', function() {
     // but now i want to acces the autocompleteObj again, but i cant :(
     // how can i access my members that i deklared in my first lines ?
     console.log(this.autocomplete.getPlace());
   });

 }

谢谢 :)

尝试这样:

MapProvider = function(mapID, autocompleteID) {
    // some other members
    MapProvider.prototype.map = null;
    MapProvider.prototype.autocomplete = null;

    // init function
    MapProvider.prototype.initAutocomplete = function() {  

        this.autocomplete = new google.maps.places.Autocomplete(this.inputField, myOptions);
        this.autocomplete.bindTo('bounds', this.map);

        var _this = this;

        google.maps.event.addListener(this.autocomplete, 'place_changed', function() {

            console.log(_this.autocomplete.getPlace());
        });

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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