Follow Flex After Dark on Twitter

Dynamic Classes

ActionScript allows classes to be defined as dynamic. A dynamic class can have public properties and functions attached to it at runtime.

   public dynamic class DynamicClass
   {
      public DynamicClass()
      {
         
      }
   }

Note the dynamic keyword used in the class declaration. So even though DynamicClass has not properties or functions defined (besides those of its super-class, Object), we can still attach properties and functions to it dynamically.

   public var dyna:DynamicClass = new DynamicClass();
   
   // add an undeclared property
   dyna.extraProperty = "Dynamically added property";
   
   // add an undeclared function
   dyna.extraFunction = function()
   {
      trace( dyna.extraProperty );
   }

In the above example, we assign an extraProperty property to the DynamicClass instance. We then add an "anonymous" function (or closure) to the object with the name extraFunction . Notice how the code inside the funtion accesses the extraProperty property; via the object name ( dyna ) rather than this .

Dynamically added functions cannot access private data/functions of the dynamic (host) object.

Related Links:

Related Docs
Recent Docs
Random Docs