Javascript 文字对象函数范围,如何将函数和上下文传递给第三方
我有这个回叫“机器”: 它被设计为对象文字Javascript 文字对象函数范围,如何将函数和上下文传递给第三方,javascript,Javascript,我有这个回叫“机器”: 它被设计为对象文字 APC.UTIL.Callback = { nn: 0, step: 0, Myargs: null, that: null, internal: function () { var the_f = that.Myargs[that.step]; if (that.step < that.nn) the_f(that.internal); else {
APC.UTIL.Callback = {
nn: 0,
step: 0,
Myargs: null,
that: null,
internal: function () {
var the_f = that.Myargs[that.step];
if (that.step < that.nn) the_f(that.internal);
else {
the_f();
that.Myargs = null;
}
that.step++;
},
loop: function () {
this.Myargs = arguments;
this.nn = this.Myargs.length - 1, this.step = 0;
that = this;
this.internal();
},
}
回调第一次调用自己,内部调用。然后F1接收函数,以便以后使用它,并再次调用“internal”,F2等。。。。
F1 F2 F3存储在Myargs数组中
已知的问题。。。缺少该或该范围。
在第二次调用时,“that”再次变为null值
我希望为调用的函数提供一个干净的解决方案。
我不想在这些方面写更多的代码。
var F1=函数循环返回{…bla bla;循环返回;}
如您所见,我只将loop_back参数作为参数编写,并使用它进行回调
我认为我的案例有点特殊,不同于与范围和对象文字相关的问题
APC.UTIL.Callback = {
nn: 0,
step: 0,
Myargs: null,
that: null,
internal: function () {
var the_f = that.Myargs[that.step];
if (that.step < that.nn) the_f(that.internal);
else {
the_f();
that.Myargs = null;
}
that.step++;
},
loop: function () {
this.Myargs = arguments;
this.nn = this.Myargs.length - 1, this.step = 0;
that = this;
this.internal();
},
}
任何帮助都将不胜感激 您将步进放置在错误的位置,如果在环回调用后调用它,将永远无法到达步进
var example = {
nn: 0,
step: 0,
Myargs: null,
that: null,
internal: function () {
var the_f = that.Myargs[that.step];
if (that.step < that.nn) {
that.step++;
the_f(that.internal);
} else {
// ...this method expects a function as a parameter.
the_f(function(){});
that.Myargs = null;
}
// will never be reached
// that.step++;
},
loop: function () {
this.Myargs = arguments;
this.nn = this.Myargs.length - 1, this.step = 0;
that = this;
this.internal();
},
}
var F1 = function (loop_back) { console.log("F1"); loop_back(); }
var F2 = function (loop_back) { console.log("F2"); loop_back(); }
var F3 = function (loop_back) { console.log("F3"); loop_back(); }
var F4 = function (loop_back) { console.log("F4"); loop_back(); }
example.loop(F1, F2, F3, F4);
这似乎很复杂,我不知道你说的回叫机是什么意思。你能发布一个真实世界的例子,以及输入/输出是什么样子的吗?@elclanrs代码就是这样:谢谢Eric。我没有考虑到这个问题@Civiltoma可能会到达,但时间对环回不利,因此在这种情况下永远不会到达。这实际上也是一样的。Myargs=null;