To make an object from an unknown class happens occasionally in OOP code. ActionScript 3.0 allows a way to both find the class name of an object, and to create an object based on a class’ (String-format) name. The definitions are found in the flash.utils package.

You can do more thorough error checking if make an object first:

import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;

public function getClassObj(obj:*):*
{
    var objClass:Class = Class(getDefinitionByName(getQualifiedClassName(obj)));
    var tempObj:* = new objClass();
    return tempObj;
}

But you can do this instead and save a variable declaration:

import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;

public function getClassObj(obj:*):*
{
    var objClass:Class = Class(getDefinitionByName(getQualifiedClassName(obj)));
    return new objClass();
}

– 30 –

Advertisements