Iterating Through AS3 Class Properties
Wayne Helman on June 11, 2009
2 Comments
Recently, we ran into a situation while building an application in Flash to generate runtime code to be consumed by Flash itself. To make a long story short, we needed a way to iterate through all the properties of any given class – or more specifically, any built in component in Flex. To accomplish this, we stumbled upon a little known reference in the AS language API:
package { import flash.display.Sprite; import flash.utils.describeType; public class IterateThroughClass { public function IterateThroughClass() { var child:Sprite = new Sprite(); var description:XMLList = describeType(child)..variable; for(var i:int; i < description.length(); i++) { //trace the name and value trace(description[i].@name + ':' + child[description[i].@name]); } } } }
A very handy utility indeed.
November 11th, 2009 @ 11:07 AM
Aren’t these instance properties, not class properties? Class properties would be Sprite.someProperty, not (new Sprite()).someProperty as you’ve shown here.
November 11th, 2009 @ 11:20 AM
Yes Justin, true. To be semantically correct, it would be a class instance. But at runtime, I don’t think there’s a way to iterate through a class without instantiating it first, although admittedly, I haven’t tried.