Flash / Flex / ActionScript/Data Type/Variable scope

Материал из Web эксперт
Перейти к: навигация, поиск

ActionScript available scopes:

   <source lang="java">

package {

 // Code here is in the global scope
 public class SomeClass {
   // Code here is in the SomeClass scope
   public static function staticMeth (  ):void {
     // Code here is in the staticMeth scope
   }
   public function instanceMeth (  ):void {
     // Code here is in the instanceMeth scope
     function nestedFunc (  ):void {
       // Code here is in the nestedFunc scope
     }
   }
 }

} // Code here is in the global scope

       </source>
   
  


Class Level (Static) Variables and Methods are accessed by using the class name followed by the object name

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       trace(ScopeTest.foo); // Displays: bar
   }
 }

}

class ScopeTest {
     public static var foo:String = "bar";
  }
       </source>
   
  


Function-Level Variables and Functions

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var myTest:ScopeTest = new ScopeTest();
       myTest.showGreeting(); // Displays: Hello, world!
    //   trace(myTest.message); // undefined
   }
 }

} class ScopeTest {

     public static var foo:String = "bar";
     public var answer:Number = 42;
     public function showGreeting():void {
        var message:String = "Hello, world!";
        trace(message);
     }

}

       </source>
   
  


Instance Level Variables and Methods are independent for each instance of the class

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var myTest:ScopeTest = new ScopeTest();
       trace(myTest.answer) //Displays: 42
   }
 }

} class ScopeTest {

     public static var foo:String = "bar";
     public var answer:Number = 42;

}

       </source>
   
  


Instance Method Scope

   <source lang="java">

package {

 public class SomeClass {
   public function instanceMeth (  ) {
     // Code here is in the instanceMeth scope
   }
 }

}

       </source>
   
  


Static Method Scope

   <source lang="java">

package {

 public class SomeClass {
   public static function staticMeth (  ) {
   }
 }

} package {

 public class SomeClass extends SomeParentClass {
   public static function staticMeth (  ) {
     // Local variables, nested functions, and namespaces defined here
     // are accessible throughout staticMeth
   }
 }

}

       </source>