0

I am fairly new with JS object contructors and I seem to have a problem here, either with how I construct the object, scope or both. The myService method does create two new properties, response and status, however when I try to access those properties they are "undefined" and I am not sure why.

var n = new myPlaces(map, myOptions);
var z = n.places();
console.log(z);   // returns object with all properites
console.log(z.response) //undefined  
console.log(z.status) // undefined


function myPlaces(map, myOptions) {     
        this.map = map;
        this.myOptions = myOptions; 
        };  

    myPlaces.prototype = {
            construtor: myPlaces,
            myService: function() {
                var self = this;
                this.service = new google.maps.places.PlacesService(this.map);
                this.service.nearbySearch(this.myOptions.request, function(results, status) {
                    self.response = results;  //response: Array[20]
                    self.status = status;     //status: "OK"
                        });
                return self;
            },
            places:function() {
                return this.myService();    
        }   
        };
user2232681
  • 839
  • 4
  • 16
  • 33
  • You are trying to access properties before they exist. Please see http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron as well. – Felix Kling Aug 15 '14 at 19:23
  • so, the short answer is that whatever I want to do with the async result I must do in the callback function? – user2232681 Aug 15 '14 at 19:48
  • Yes. You can make `places` accept a callback function and pass the result to the callback function, or use promises. – Felix Kling Aug 15 '14 at 20:01

0 Answers0