Using XML and ActionScript with Flex Applications - Getting the Class by Name (Page 5 of 5 )
If you have a class name, you can retrieve a reference to the class using the getDefinitionByName() function. The function requires a string parameter specifying a class name, and it returns an Object type. The function returns an Object type rather than aClasstype because it could also theoretically return a reference to a function if you pass it a fully qualified function name (e.g.,flash.util.getTimer). If you’re certain that you’re retrieving a class reference, you can cast the return value toClass, as in the following example:
var classReference:Class = Class(getDefinitionByName("flash.net.URLLoader"));
Once you’ve retrieved a reference to a class, you can create anewinstance, as follows:
var instance:Object = new classReference();
Obviously you can use the return value fromgetQualifiedClassName()orgetQualifiedSuperclassName()in conjunction withgetDefinitionByName(), as in the following example:
var loader:URLLoader = new URLLoader(); var className:String = getQualifiedClassName(loader); var classReference:Class = Class(getDefinitionByName(className)); var instance:Object = new classReference();
You can use describeType() to return a description of all the events, public properties, and public methods of an object. Simply pass the method a reference to the object you want to introspect. The method returns an XML object that details the class name, superclass, various class settings, implemented interfaces, constructor signature, public method signatures, and public properties descriptions.
The following example retrieves the description for aURLLoaderobject:
var loader:URLLoader = new URLLoader(); var description:XML = describeType(loader); trace(description);
With some work, you can create complex systems that use objects to create sophisticated and dynamic applications.
In this chapter, we discussed the fundamentals of ActionScript 3.0. ActionScript is the ECMAScript-standard-based programming language used by Flex applications. Although the topic of ActionScript is far too complex to discuss comprehensively in one chapter, we have covered many of the basics you’ll need to get started writing ActionScript code, including where to place the code, basic syntax, common data types, how to write classes, the event model, error handling, working with XML, and reflection.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.