关于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