javascript递归类:未定义的方法
javascript recursive class: undefined method
我有一个JavaScript类,用来帮助处理promise。首先,您将函数添加到数组中,然后它执行这些函数,弹出它们并调用自己来执行下一个函数。在数组的末尾,它解决了这个问题。我的希望是将该解决方案一直传播到递归调用的堆栈中。这将允许您使用一组简单的命令强制多个异步函数按顺序运行。此外,使用逻辑来修改ansync函数的流。
function Sequencer() {
this.functionSequence = [];
this.addFunction = function (func) {
this.functionSequence.push(func);
}
this.getFunctionSequence = function () {
return functionSequence;
}
this.executeAll = function () {
var functionList = this.functionSequence;
var deferred = $q.defer();
if (functionList.length > 0) {
functionList[0]().then(function (result) {
if (result) {
functionList.splice(0, 1);
executeAll().then(function (resultInner) {
if (resultInner == true) {
deferred.resolve(true);
} else {
deferred.resolve(false);
}
});
} else {
functionList = [];
deferred.resolve(false);
}
});
} else {
deferred.resolve(true);
}
return deferred.promise;
}
}
我正在获取ReferenceError:"executeAll"未定义在这个脚本中,在拼接之后的递归调用行"executeAll"上
数组中的第一个函数正在执行(我用模态弹出窗口测试它),当它解析时,它会碰到拼接,然后它会在executeAll行上抛出错误。我对函数的定义有误吗?作为递归函数,我是否正确地调用了它?
使用this.executeAll
-假设this
是正确的,但事实并非如此,因此您也需要考虑到这一点。。。类似于executeAll顶部的var self = this
,然后调用self.executeAll
this.executeAll = function() {
var functionList = this.functionSequence;
var deferred = $q.defer();
var self = this; // save reference to this
if (functionList.length > 0) {
functionList[0]().then(function(result) {
if (result) {
functionList.splice(0, 1);
// need to use self here because "this" is not the "this" we want
self.executeAll().then(function(resultInner) {
if (resultInner == true) {
deferred.resolve(true);
} else {
deferred.resolve(false);
}
});
} else {
functionList = [];
deferred.resolve(false);
}
});
} else {
deferred.resolve(true);
}
return deferred.promise;
};
this
不是你"想要"的this
的原因是this
在javascript中的工作方式-关于使用this
的堆栈交换有很多信息-我很快就会找到并链接一个好的答案
我提供这个替代代码
this.executeAll = function() {
return this.functionSequence.reduce(function(promise, item) {
return promise.then(function(result) {
if (result) {
return item();
}
else {
throw "Fail"; // throw so we stop the chain
}
});
}, Promise.resolve(true))
.then(function(result) {
this.functionSequence = []; // clear out the added functions
return true; // fulfilled value is true as per original code
}.bind(this), function(err) {
this.functionSequence = []; // clear out the added functions
if (err == "Fail") {
return false; // convert the "Fail" to a fullfilled value of false as per original code
}
else {
throw err; // any other error - re-throw the error
}
}.bind(this))
};
相关文章:
- 骨干's Router.execute(callback,args,name)方法获取未定义的名称
- React路由器错误-'无法调用方法'getRouteAtDepth'的未定义'
- 使用javascript ajax post方法的未定义偏移PHP错误
- jQuery验证器添加方法未定义
- 定义的静态方法未定义
- jQuery Validate() 和 Valid() 方法未定义或不起作用
- 为什么内部函数中当前对象的方法未定义
- 如何解决“不能调用方法...未定义“错误
- 得到"无法调用方法..未定义的“;JavaScript和Mustache出现错误
- JavaScript方法未定义
- initialize()方法未定义
- ExpressJS PUT方法未定义的对象问题
- JavaScript - Prototype方法未定义
- 映射内匿名函数内的方法未定义
- 在事件处理程序中调用方法未定义
- 在setTimeOut函数中传递多个参数会抛出Uncaught ReferenceError:方法未定义(匿名函数)
- Javascript对象方法未定义
- app()方法未定义
- 未捕获的类型错误:不能调用方法'未定义使用Ckeditor
- QuickTime对象的SetURL方法未定义