flash.utils.getDefinitionByName.as Maven / Gradle / Ivy
package flash.utils {
import joo.getQualifiedObject;
/**
* Returns a reference to the class object of the class specified by the name
parameter.
* @param name The name of a class.
*
* @return Returns a reference to the class object of the class specified by the name
parameter.
*
* @throws ReferenceError No public definition exists with the specified name.
*
* @example The following example uses the class GetDefinitionByNameExample
to create an orange square on the stage. This is accomplished using the following steps:
* - Variables for the background color of orange and size of 80 pixels are declared, which will later be used in drawing the square.
* - Within the constructor, a variable
ClassReference
of type Class is assigned to Sprite.
* - An instance of ClassReference called
instance
is instantiated.
* - Since
instance
is, by reference, a Sprite object, a square can be drawn and added to the display list using the methods available to Sprite.
*
* package {
* import flash.display.DisplayObject;
* import flash.display.Sprite;
* import flash.utils.getDefinitionByName;
*
* public class GetDefinitionByNameExample extends Sprite {
* private var bgColor:uint = 0xFFCC00;
* private var size:uint = 80;
*
* public function GetDefinitionByNameExample() {
* var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class;
* var instance:Object = new ClassReference();
* instance.graphics.beginFill(bgColor);
* instance.graphics.drawRect(0, 0, size, size);
* instance.graphics.endFill();
* addChild(DisplayObject(instance));
* }
* }
* }
*
*/
public function getDefinitionByName(name:String):Object {
var clazz:* = getQualifiedObject(name.replace("::","."));
if (typeof clazz !== 'function') {
throw new ReferenceError(name);
}
return clazz;
}
}