Macromedia flex 2-migrating applications to flex 2 Manual De Usuario
176
Migration Patterns
Variable enumeration and object
introspection
introspection
In Flex 1.x (ActionScript 2.0), using a
for-in
loop on an object let you enumerate over all
properties on an object. In Flex 2 (which uses ActionScript 3.0), only dynamically added
properties are enumerated by
properties are enumerated by
for-in
loops. Declared variables and methods of classes are not
enumerated in
for-in
loops. This means that most classes in the ActionScript API do not
display any properties in a
for-in
loop. The generic type Object is still a dynamic object and
displays properties in a
for-in
loop.
To list all of the public properties and methods of a class or class instance, use the
describeType()
method and to parse the results use the E4X API. The
describeType()
method is in the flash.system package. The method’s only parameter is the object that you
want to introspect. You can pass any ActionScript value to it, including all available
ActionScript types such as object instances, primitive types such as uint, and class objects. The
return value of the
want to introspect. You can pass any ActionScript value to it, including all available
ActionScript types such as object instances, primitive types such as uint, and class objects. The
return value of the
describeType()
method is an E4X XML object that contains an XML
description of the object’s type. For more information about using E4X, see the Flex 2
Developer’s Guide.
Developer’s Guide.
The following example introspects the Button control and prints the details to TextArea
controls:
controls:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="getDetails()">
<mx:Script><![CDATA[
import flash.system.*;
import flash.system.*;
public function getDetails():void {
// Get the Button control’s E4X XML object description:
var classInfo:XML = describeType(button1);
var classInfo:XML = describeType(button1);
// Dump the entire E4X XML object into ta2:
ta2.text = classInfo.toString();
ta2.text = classInfo.toString();
// List the class name:
ta1.text = "Class " + classInfo.@name.toString() + "\n";
ta1.text = "Class " + classInfo.@name.toString() + "\n";
// List the object's variables, their values, and their types:
for each (var v:XML in classInfo..variable) {
for each (var v:XML in classInfo..variable) {
ta1.text += "Variable " + v.@name + "=" + button1[v.@name] + " (" +
v.@type + ")\n";
}
}