Reflection
ActionScript 3.0 supports class reflection using the following
functions in the flash.utils
package:
getQualifiedClassNamegetQualifiedSuperclassNamegetDefinitionByNamedescribeType
Getting the Class Name
You can retrieve the name of the class for which an object is an
instance using the getQualifiedClassName() function. The function
requires that you pass it a reference to an object; it then returns the
fully qualified class name.
var loader:URLLoader = new URLLoader(); var className:String = getQualifiedClassName(loader); trace(className); // Displays flash.net::URLLoader
If you want to retrieve the fully qualified superclass name for an
object, you can use the getQualifiedSuperclassName()
function:
var loader:URLLoader = new URLLoader(); var className:String = getQualifiedSuperclassName(loader); trace(className); // Displays flash.events::EventDispatcher
Getting the Class by Name
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
fully qualified class name, and it returns an Object type. The function returns an Object type rather than a Class type 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 to Class,
as in the following example:
var classReference:Class = Class(getDefinitionByName("flash.net.URLLoader")); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access