0

我想在 Flex 中启用所有编译器警告以在我的代码中解决它们。但是有一个警告,我不知道如何解决它。这是一些示例代码:

package lib
{
    import flash.events.NetStatusEvent;
    import flash.net.NetConnection;

    public class player
    {
        private function tmp(event:NetStatusEvent):void
        {
        }

        public function player():void
        {
            super();
            var connection:NetConnection = new NetConnection();
            connection.addEventListener(NetStatusEvent.NET_STATUS, tmp);
        }
    }
}

在使用 -warn-scoping-change-in-this 编译时,我收到以下警告:

/var/www/test/src/lib/player.as(16): col: 59 Warning: Migration issue: Method tmp will behave differently in ActionScript 3.0 due to the change in scoping for the this keyword. See the entry for warning 1083 for additional information.

            connection.addEventListener(NetStatusEvent.NET_STATUS, tmp);

将 tmp 作为函数放入 player() 将起作用,但这不是我想要的。我什至尝试使用 this.tmp 作为回调,但没有区别。有人知道如何解决这个编译器警告吗?

4

2 回答 2

1

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/compilerWarnings.html

这是一个代码迁移警告。当对象的方法被用作值时会生成此警告,通常用作回调函数。在 ActionScript 2.0 中,函数在调用它们的上下文中执行。在 ActionScript 3.0 中,函数始终在定义它们的上下文中执行。因此,变量和方法名称被解析为回调所属的类,而不是相对于调用它的上下文,如下例所示:

class a 
{ 
   var x; 
   function a() { x = 1; } 
   function b() { trace(x); } 
}

var A:a = new a();
var f:Function = a.b; // warning triggered here
var x = 22;
f(); // prints 1 in ActionScript 3.0, 22 in ActionScript 2.0
于 2013-02-22T10:01:57.933 回答
0

该警告只是为了让您知道,如果您将代码从 AS2 迁移到 AS3(编译器无法事先知道),代码的行为可能已经改变。当您将代码从 AS2 迁移到 AS3 时,您应该启用编译器选项。-warn-scoping-change-in-this

因此,正如我在评论中所说,您不必担心该警告,因为显然看到您的代码不是您的情况,并且您不需要启用该编译器选项。

于 2013-02-22T10:36:02.403 回答