在事件侦听器中访问原型成员

时间:2012-11-18 13:21:56

标签: javascript google-maps event-handling prototype

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

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());
   });

 }

谢谢:)

1 个答案:

答案 0 :(得分:0)

试试这样:

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());
        });

    }
}