Flash / Flex / ActionScript/Class/Dynamic object

Материал из Web эксперт
Версия от 08:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Call setPropertyIsEnumerable in dynamic object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var info:Object = new Object(  );
        info.city = "Toronto";
        info.country = "Canada";
        
        
        info.setPropertyIsEnumerable("city", false);
        
        for (var detailName:* in info) {
          trace(info[detailName]);
        }
    }
  }
}



Create dynamic class

 
dynamic class Employee {
  public var startDate:Date;
  private var age:int;
}
class Report {
  public function Report (employee:Employee) {
    employee.doReport = function (  ):void {
      trace(this.startDate);  // Access to public variable allowed
      trace(this.age);        // Access to private variable denied
    }
  }
}



Dynamic ActionScript: All dynamic instance variables are untyped and public

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var person:Person = new Person(  );
        person.eyeColor = "brown";
         
        trace(person.eyeColor);  // Displays: brown
        trace(person.age);  // Displays undefined because the person object
    }
  }
}
dynamic class Person {
}



Dynamically Adding New Behavior to an Instance

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var info:Object = new Object(  );
        info.city = "Toronto";
        info.country = "Canada";
        
        info.getAddress = function (  ):String {
          return this.city + ", " + this.country;
        }
        trace(info.getAddress(  ));
        
    }
  }
}



Dynamic References to Variables and Methods

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var info:Object = new Object(  );
        info["city"] = "Toronto";
        info["country"] = "Canada";
        var detail:String = "city";
        trace(info[detail]);  // Displays: Toronto
    }
  }
}



Making Lookup Tables with Object Literals

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var info:Object = {city:"Toronto", country:"Canada"};
        
        //The preceding code is identical to the following code:
        var info:Object = new Object(  );
        info.city = "Toronto";
        info.country = "Canada";
        
    }
  }
}



Processing Dynamic Instance Variables with for-each-in and for-in Loops

 
for each (variableOrElementValue in someObject) {
  statements
}

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var info:Object = new Object(  );
        info.city = "Toronto";
        info.country = "Canada";
        
        for each (var detail:* in info) {
          trace(detail);
        }
    }
  }
}



Using Dynamic Instance Variables to Create Lookup Tables

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var meal:Object = new Object(  );
        meal.t  = "t";
        meal.b = "b";
        meal.c    = "c";
        
        trace(meal); //[object Object]
    }
  }
}