我正在尝试在Javascript中实现异步方法队列,如this blog post
中所示这是我到目前为止所拥有的:
function Queue() {
this._methods = [];
this._response = null;
this._flushed = false;
}
(function(Q){
Q.add = function (fn) {
if (this._flushed) fn(this._response);
else this._methods.push(fn);
}
Q.flush = function (response) {
if (this._flushed) return;
this._response = response;
while (this._methods[0]) {
this._methods.shift()(response);
}
this._flushed = true;
}
})(Queue.prototype);
我似乎无法像宣传的那样让它工作,尽管代码对我来说是正确的。当我调用flush函数时,我在行this._methods is undefined
上获得while (this._methods[0]) {
。
答案 0 :(得分:3)