AS3 - 关于符号绑定的问题
我是 AS3 的新手,无法弄清楚为什么这个循环没有按照它“应该”的方式运行。
for each (var s in [_set, _set.otherSet]) {
for each (var f in [s.frame_top_mc, s.frame_bottom_mc]) {
f.addEventListener(MouseEvent.CLICK, function( ):void {
_score[f.category] += 1;
madeSelection(f);
});
}
}
如何为每个匿名函数提供对 f
表示的每个对象的引用,而不是每次对 f
的简单引用?
具体来说,为什么匿名函数的每个副本都绑定到对 f
的单个引用?在这方面,AS3 与 JavaScript 到底有何不同(我应该说为什么)?
I'm new to AS3, and can't figure out why this loop isn't behaving the way it "should."
for each (var s in [_set, _set.otherSet]) {
for each (var f in [s.frame_top_mc, s.frame_bottom_mc]) {
f.addEventListener(MouseEvent.CLICK, function( ):void {
_score[f.category] += 1;
madeSelection(f);
});
}
}
How can I give each anonymous function a reference to each object represented by f
, rather than a simple reference to f
each time?
Specifically, why is it that each copy of the anonymous function gets bound to a single reference to f
? How (I should say why) exactly does AS3 differ from JavaScript in this regard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它需要像这样:
问题是您的闭包正在关闭循环变量 f 本身,而不是循环中使用 f 引用的每个事物。循环完成后,f 将作为循环列表中最后一项的引用。 f 在创建闭包时不会取消引用,而是在执行时取消引用。
It needs to be like this:
The problem is that your closure is closing over the loop variable f itself rather than each thing f is being used to reference within the loop. After the loop completes, f is left as reference to the last thing in the list you looped over. f is not being de-referenced when the closure is created, but when it's executed.