Actionscript 3“这个”问题
有人可以指出为什么以下代码在第一种情况下会失败:
CASE 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();
});
CASE 2
this.gallery.addEventListener(GalleryEvent.WHATEVER, function(event:*) {
// This works fine
gallery.someAction();
})
关于 this
的使用是否有任何规则在这种情况下?
Could someone point out why does the following code fail in the first case:
CASE 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();
});
CASE 2
this.gallery.addEventListener(GalleryEvent.WHATEVER, function(event:*) {
// This works fine
gallery.someAction();
})
Are there any rules regarding this
usage in such cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为“作用域链”。在 ActionScript 3(与 ECMAScript 兼容 - JavaScript 具有下面描述的相同行为)中,有一个内置的“位置”列表,用于解析任何命名变量,称为作用域链。例如,当在类的“正常”方法中时,作用域链如下所示:
this
相同)Math
)但是当你在一个匿名内部函数中时,作用域链的顶部多了一个条目,代表了当时作用域中的方法匿名函数已创建。例如,假设您有以下代码:
在这种情况下,当您位于“此处作用域链上有什么”的行时,作用域链看起来像这样(下面的第一个作用域是要检查的第一个位置) ):
f()
的一个实例,带有变量localvar
和innerfunc
C
的当前实例C
重要的是要认识到,当代码行
var innerfunc:Function = function()...
执行时,它正在创建 Function 对象动态地,并动态地设置其作用域链。例如,假设您有这段(令人困惑的)代码,给定一个参数an_arg
,它返回一个函数,在调用该函数时,将打印 an_arg 的值:调用
返回的每个函数>f()
有自己的作用域链,指向f()
的不同实例。所以:避免这个问题的常见方法,如果你真的想在内部函数中引用
this
而不是依赖作用域链的魔力,那就是创建一个名为的临时变量self
在外部函数中 - 在您的示例代码的情况下,它看起来像这样:我可以永远继续这个主题 - 我发现它很有趣:-)
This is because of the "scope chain". In ActionScript 3 (which is ECMAScript compliant -- JavaScript has the same behavior described below), there is a built-in list of "places" that is used to resolve any named variable, called the scope chain. E.g. when in a "normal" method of a class, the scope chain looks like this:
this
)Math
)But when you are in an anonymous inner function, the scope chain has one more entry at the top, representing the method that was in scope at the point in the time when the anonymous function was created. So for example, suppose you have this code:
In this case, when you are the the line that says "what is on the scopechain here", the scopechain looks like this (with the first scope below being the first place that will be checked):
f()
, with variableslocalvar
andinnerfunc
C
C
It's important to realize that that when the line of code
var innerfunc:Function = function()...
executes, it is creating the Function object on the fly, and setting its scope chain on the fly. So for example, suppose you had this (confusing) code, which, given an argumentan_arg
, returns a Function which, when invoked, will print the value of an_arg:Each function returned by calls to
f()
has its own scope chain, pointing to different instances off()
. So:The common way to avoid this problem, if you really want to refer to
this
in the inner function rather than relying on the magic of the scope chain, is to create a temporary variable calledself
in the outer function -- in the case of your sample code, it would look like this:I could go on forever on this subject -- I find it fascinating :-)
因为您传递此函数
function(event:*)
这意味着它是在该范围内执行的。因此,当它尝试查找
this
时,它会查找event.gallery.someAction();
当您使用
gallery
时,它会尝试查找它在全局变量列表中。验证这一点的一种方法是添加跟踪(this)并查看会发生什么。
because you're passing this function
function(event:*)
which means it is executed in that scope.so when it tries to look for
this
it is looking forevent.gallery.someAction();
when you use
gallery
, it tries to find it within the list of global variables.One way to verify this is to add a trace(this) and see what happens.