關於this question,我試圖添加一個回調來獲取數據。所以,我想這一點:Javascript中的遞歸異步回調
var subgroupIds = [];
var that = this;
this.getSubGroups = function (groupId,callback) {
var anotherObject = this;
this.getGroups("groupId="+groupId, function(groups) {
if ($.isEmptyObject(groups)) {
return;
} else {
$.each(groups, function(index,group) {
subgroupIds.push(group.id);
that.getSubGroups(group.id);
});
anotherObject.callback(group.id);
}
});
}
,我想我有以前的問題後更好地理解封閉的,但我想我不......,我發現了以下錯誤:
Uncaught TypeError: Object [object Window] has no method 'callback'
我在這裏做錯了什麼?
編輯
這裏的getGroups的內容:
this.getGroups = function(filter,callback,error_callback) {
this.getJSON('/'+apiVersion+'/groups/',function(data){
// run through the filter engine
output = runFilter(data, filter);
callback(output);
},error_callback);
}
也許'this.getGroups'應該是'anotherObject.getGroups'? – 2013-02-28 14:34:09
爲什麼使用'anotherObject'而不是'that'?爲什麼你回調一個屬性,你不想回調函數參數? – Bergi 2013-02-28 14:37:20
什麼是'這個'''那個'?當你聲明'this.getSubGroups'時,'this'是指'window',所以你沒有做正確的事情來讓'this'指向正確的東西。然後,'anotherObject'就是指同一個'window'。另外,你爲什麼試圖使用'anotherObject.callback(group.id)'?你不只是想'回調(group.id);'? – Ian 2013-02-28 14:37:24