Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
691 views
in Technique[技术] by (71.8m points)

actionscript 3 - Is As3 const initialized during compile or runtime?

I'm using ActionScript and I saw there is the possibility of using const attribues.

I'm with a doubt:

  • Is As3 const attributes initialized during compile or runtime?
question from:https://stackoverflow.com/questions/65889791/is-as3-const-initialized-during-compile-or-runtime

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok, let's spill some light on the flow of things with constants. I devised a pretty simple and self-explanatory class:

package 
{
    import flash.display.Sprite;
    
    public class Kunst extends Sprite
    {
        public const A:int = int(trace("Init A:", A, B, C, D)) + 10;
        public const B:String = initB() + String(trace("Init B:", A, B, C, D)) + initB();
        public const C:int = 10 + 10;
        
        static public const D:String = String(trace("Init D:", D, E, F)) + initD();
        static public const E:Boolean = initE() && Boolean(trace("Init E:", D, E, F)) || initE();
        static public const F:int = 10 + 10 + 10;
        
        public function Kunst() 
        {
            super();
            
            trace("Check ABC:", A, B, C);
            trace("Check DEF:", D, E, F);
        }
        
        private function initB():String
        {
            trace("Init B2:", A, B, C, D);
            return "B";
        }
        
        static private function initD():String
        {
            trace("Init D2:", D, E, F);
            return "D";
        }
        
        static private function initE():Boolean
        {
            trace("Init E2:", D, E, F);
            return true;
        }
    }
}

It outputs the following:

Init D: null false 30
Init D2: null false 30
Init E2: undefinedD false 30
Init E: undefinedD false 30
Init E2: undefinedD false 30

Init A: 0 null 20 undefinedD
Init B2: 10 null 20 undefinedD
Init B: 10 null 20 undefinedD
Init B2: 10 null 20 undefinedD

Check ABC: 10 BundefinedB 20
Check DEF: undefinedD true 30

Lets comprehend the results:

  1. All static constants go before the very first line of the application code executes. Before non-static constants of the main document class as well. That all was expected.
  2. Among the same ones (i.e. either static or non-static) first initialize those that can be calculated right away, ones with outright value or simple formula. They, actually, could even be defined at the compile time.
  3. The rest, the ones within the same (static or non-static) group, go by the exact order they are declared.
  4. I thought you can initialize constants in the constructor block, but it turns out you can not. Maybe it was so in AS2, I don't really remember now.
  5. At the moment the constructor starts executing, all the class member constants are already initialized.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...