Dynamic Classes in Flex and Actionscript

Skill

Dynamic Classes in Flex and Actionscript

Posted in:

I have known about dynamic classes in Actionscript for a while but I just recently started playing around with them. Basically they are declared classes that can have additional properties tacked on to them at runtime. To add this ability to a class you simply need to add the keyword dynamic to the class definition. So below we have very simple dynamic class.

package
{
  public dynamic class CoolObject
  {
    public var title:String;
    public var count:int;
   
    public function traceMe():void
    {
      var out:String = "";
      out = "title: " + title + "\n";
      out += "count: " + count;
      trace(out);
    }
  }
}

Nothing is different so far except for the dynamic keyword and we can treat the class just like normal - setting properties and calling methods. So something like what's below will work just fine.

var myCoolObject:CoolObject = new CoolObject();
myCoolObject.title = "Mr Cool";
myCoolObject.count = 30;

myCoolObject.traceMe();
// Output:
// title: Mr Cool
// count: 30

However, because it is dynamic, we can also do things like set new properties and add functions. The following code adds a new property and function. It then calls the new function.

myCoolObject.favorite = "The Fattest";
myCoolObject.traceTheCoolest = function ():void
{
  trace(this.favorite + " is the Coolest");          
};

myCoolObject.traceTheCoolest();
// Output:
// The Fattest is the Coolest

We can also loop through all the dynamic properties in the object. Notice from the output that we only get the added properties and functions.

myCoolObject.lamest = "The Reddest";
myCoolObject.old = 50;

for (var i:String in myCoolObject)
{
  trace("Property " + i + ": " + myCoolObject[i]);
}
// Output:
// Property traceTheCoolest: function Function() {}
// Property lamest: The Reddest
// Property old: 50
// Property favorite: The Fattest

Now I am sure there are many more possibilities for uses for dynamic classes. I am interested in hearing the ways that you all have utilized this feature. Also if you have any questions feel free to leave a comment. Until next time, keep coding and commenting.

Ariel Sommeria-klein
10/04/2008 - 09:10

This reminds me of Actionscript 2. Unfortunately I've never seen any application of this that wasn't a case of lazy design.
Ariel

reply

imperez
02/13/2009 - 11:31

Yes this is pretty much how AS2 worked. It was kind of a blessing and a curse I'd say. Now in AS3, I can't say that I have used this too much except for when I wanted a class to hold dynamic data. The class would dynamically add the properties that are taken from an xml object and then I'd later access those properties later in my application. It served as a better way than accessing/parsing the xml directly.

That's the only time that I have seen a real reason to use dynamic classes.

reply

Lead Bi
03/09/2009 - 19:16

Is there a way to retrieve code for the function body from server asynchrnously, and assign it to some dynamic class's method property.

Basically, allowing for the behavior of class to completely change on the fly. So in the example above, would there be a way to retrieve the new definition for function

function ():void
{
trace(this.favorite + " is the Coolest");
};

to be retrieved from server on the fly, via HTTPService call

reply

vema
11/30/2009 - 21:54

it 's very nice answer.......

reply

mahesh
04/06/2010 - 07:03

//9.57.11.166:9080/sales/gss/ProxyService/UserLoginProxyService?applicationId='+applicationId+'&appSecId='+appSecId+'&emailId='+emailId+'&country='+country+"&callback=?",function(data) {}

var ma:String = "http://9.57.11.166:9080/sales/gss/ProxyService/UserLoginProxyService";

var temp:Object = new Object();
temp.applicationId = 27;
temp.appSecId = 44;
temp.emailId = "maheshre@in.ibm.com";
temp.country = "in";
temp.callback = function (data){}

myHttpS.url = ma;
myHttpS.send(temp);

i need to place temp.callback = some function ...... i menctioned prototype of that url.

please let me know how to pass 4th parameter i.e that as a function with argument....

e.maheswarareddy@gmail.com

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.

Sponsors