ActionScript: what's the defined order for initializing internal/static/instance variables?

你离开我真会死。 提交于 2019-12-24 15:53:25

问题


Consider a .as file which looks like this:

package foo {
class Foo {
    public static var a:* = getA();
    public static var b:* = getB();
    public var c:* = getC();

    public static function d():* { ... }

    public function Foo() {
        trace("initializing");
    }
}
}

// internal functions/variables
var e:* = getD();
function f():* { ... }

What is the defined order for initializing each of the variables/functions a..f?

(I know I can do experiments to find out… But I'm looking for the actual specified definition)


回答1:


Well, the tricky part is static initialization.

All static methods and variables are declared prior to any execution. Static methods are "instantly" initialized, in the sense that you can call them at any time. Then static variables are initialized in order of appearance. Then all static initialization is performed in order of appearence:

package {
    import flash.display.Sprite;
    public class Main extends Sprite {      
        __init(c,'static');//"static initialization b" because all variables are initialized prior to static initialization
        public static var a:String = __init(b);//"variable initialization null", because b is declared, but not initialized
        public static var b:String = __init('b');//"variable initialization b" for obvious reasons
        public static var c:String = __init(b);//"variable initialization b" because b is now initialized
        public static function __init(s:String, type:String = 'variable'):String {
            trace(type, 'initialization', s);
            return s;
        }
        public function Main():void {}      
    }
}

But in general, my advise is not rely on it, unless you really have to.



来源:https://stackoverflow.com/questions/4627631/actionscript-whats-the-defined-order-for-initializing-internal-static-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!