访问脚本变量属性的意外行为
我尝试执行以下 actionscript3 程序,并且对 f() 函数调用的结果感到惊讶。我期望 f() 的结果是“1”或至少“未定义”,但“0”值对我来说没有任何意义。
如果您有这种行为,我会很高兴能得到一个很好的解释,或者知道您是否认为这种行为是“正常”的。 我想准确地说,我正在研究 Action Script 程序的行为,以便了解 AVM2 的真正工作原理,因此我并不要求等效的代码来做同样的事情。因此,如果您还有其他棘手的例子,我也很感兴趣。
package {
import flash.display.Sprite;
public class S2 extends Sprite {
public function f():* {
return x;
}
public static function fs():*{
return x;
}
}
}
var x:int = 1 ;
var a:S2 = new S2();
var g:Function = a.f;
var gs:Function = S2.fs;
trace("tracing(g)...:"+g()); //tracing(g)...:0
trace("tracing(gs)...:"+gs()); //tracing(gs)...:1
注意:我使用以下命令行编译了该程序:
mxmlc -debug -static-link-runtime-shared-libraries=true -output S2.swf -- S2.as
I tried to execute the following actionscript3 program and I am surprised of the result of the call to f() function. I was expecting that the result of f() was "1" or at least "undefined" but the "0" value does not makes any sense for me.
I will be pleased to have a good explanation of this behavior if you have one, or to know if you consider this behavior as "normal".
I want to precise that I'm studying the behavior of Action Script programs in order to understand how the AVM2 really works and therefore I am not asking equivalent code to do the same thing. As a consequence if you have other tricky examples I am also interested.
package {
import flash.display.Sprite;
public class S2 extends Sprite {
public function f():* {
return x;
}
public static function fs():*{
return x;
}
}
}
var x:int = 1 ;
var a:S2 = new S2();
var g:Function = a.f;
var gs:Function = S2.fs;
trace("tracing(g)...:"+g()); //tracing(g)...:0
trace("tracing(gs)...:"+gs()); //tracing(gs)...:1
Note: I compiled this program with the following command line:
mxmlc -debug -static-link-runtime-shared-libraries=true -output S2.swf -- S2.as
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
x
变量与您从S2.f()
S2
extends 返回的x
存在于不同的作用域中Sprite
,它又扩展了DisplayObject
,后者已经具有 ax 属性。这就是您要返回的内容。
如果将变量名称更改为
myX
之类的名称,您将收到预期的错误。这样做会改变返回的内容:
Your
x
variable exists in a different scope than thex
you are returning fromS2.f()
S2
extendsSprite
, which in turn extendsDisplayObject
, which already has a x property.This is what you are returning.
If you change the variable name to something like
myX
you will get an error a expected.Doing this will change what is returned: