2012-11-18 44 views
0

我如何在事件監聽器中訪問對象的成員? 下面是一個例子,我想這使得它更容易理解;)在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()); 
    }); 

} 

謝謝:)

回答

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

    } 
} 
+0

這個作品非常好,謝謝! – chaosbohne