我对JS对象构造函数相当新,我似乎在这里遇到了问题 我如何构建对象,范围或两者。 myService方法确实创建了两个新的 属性,响应和状态,但是当我尝试访问这些属性时 "未定义"我不确定为什么。
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();
}
};