我想知道是否有一种优雅的方式来执行以下代码,而不必先调用父对象“that”。如果我尝试在 ajax 请求中使用“this”,它显然会引用 Ajax 对象。
至少这就是我认为的意思。
var ObjectA = Class.create();
ObjectA.prototype = {
initialize: function() {
//Workaround I use
that = this;
},
getData: function(bounds) {
//ajax to get some data
url = "http://www.data.com/";
new Ajax.Request(url, {
method: 'get',
onSuccess: function(response) {
// Handle the response content...
that.workData(response.responseText);
//THIS IS MY DOUBT.
//How do I access the parent object without having to previously calling it "that" first?
}
});
},
workData: function(data){
//do something with the data
}
}
var test = new ObjectA();
test.getData();