【发布时间】:2011-01-19 00:45:54
【问题描述】:
有人能指出为什么以下代码在第一种情况下会失败:
案例 1
// In a constructor
this.gallery = new Gallery();
addChild(this.gallery);
this.gallery.addEventListener(GalleryEvent.WHATEVER, function(event:*) {
// When this callback fires, there is a fail:
// because there is no 'this.gallery'.
this.gallery.someAction();
});
案例 2
this.gallery.addEventListener(GalleryEvent.WHATEVER, function(event:*) {
// This works fine
gallery.someAction();
})
在这种情况下,this 的使用是否有任何规则?
【问题讨论】:
-
尝试使用匿名函数不添加监听器。
-
@George 是的,它有效。我纯粹对为什么会发生这种情况感兴趣。
-
已经有两个很好的答案,但是在 AS3 的上下文中阅读闭包也是值得的。
标签: flash actionscript-3 actionscript this