访问脚本变量属性的意外行为

发布于 2024-12-14 11:02:35 字数 802 浏览 0 评论 0原文

我尝试执行以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-12-21 11:02:35

您的 x 变量与您从 S2.f()

S2 extends 返回的 x 存在于不同的作用域中Sprite,它又扩展了 DisplayObject,后者已经具有 ax 属性。
这就是您要返回的内容。

如果将变量名称更改为 myX 之类的名称,您将收到预期的错误。

这样做会改变返回的内容:

var a:S2 = new S2();
a.x = 10;
trace(a.f()); // will trace 10

Your x variable exists in a different scope than the x you are returning from S2.f()

S2 extends Sprite, which in turn extends DisplayObject, 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:

var a:S2 = new S2();
a.x = 10;
trace(a.f()); // will trace 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文