How to clone (duplicate) an object in ActionScript 3
For a project I needed to clone an object of unknown type. And by clone I mean to create a new instance of that same type and then fill out all its properties (including getters and setters) to mirror the original object.
Thanks to a friend, I discovered the describeType function in AS3. But this alone will only take care of the copying part. To create an object of the same type as another one we use getDefinitionByName.
Although Flash reflection is pretty basic, with a little work it will do the trick.
Get the application files.
Here's the code:
< ?xml version="1.0" encoding="utf-8"?><mx :Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" creationComplete="init()"></mx><mx :Script> < ![CDATA[ import mx.controls.Alert; private var source:DataObject = new DataObject(); private var cloneObject:DataObject; private function init():void { source.name = 'John Doe'; source.howMany = 4.5; source.when = new Date(0); source.complexProp = new DataObject(); source.complexProp.name = 'Name in sub-object'; cloneObject = UtilFunctions.clone(source) as DataObject; Alert.show("Clone:\nname = " + cloneObject.name + "\nhowMany = " + cloneObject.howMany + "\nwhen = " + cloneObject.when + "\ncomplexProp.name = " + cloneObject.complexProp.name); } /** * describeType will produce this (for a DataObject instance): * * <type name="DataObject" base="Object" isDynamic="false" isFinal="false" isStatic="false"> <extendsclass type="Object"/> <accessor name="isHandicap" access="writeonly" type="Boolean" declaredBy="DataObject"/> <variable name="howMany" type="Number"/> <accessor name="complexProp" access="readwrite" type="DataObject" declaredBy="DataObject"/> <variable name="name" type="String"/> <variable name="when" type="Date"/> * * */ ]]> </mx>
package{ import flash.utils.describeType; import flash.utils.getDefinitionByName; import flash.utils.getQualifiedClassName; public class UtilFunctions { public static function newSibling(sourceObj:Object):* { if(sourceObj) { var objSibling:*; try { var classOfSourceObj:Class = getDefinitionByName(getQualifiedClassName(sourceObj)) as Class; objSibling = new classOfSourceObj(); } catch(e:Object) {} return objSibling; } return null; } public static function clone(source:Object):Object { var clone:Object; if(source) { clone = newSibling(source); if(clone) { copyData(source, clone); } } return clone; } public static function copyData(source:Object, destination:Object):void { //copies data from commonly named properties and getter/setter pairs if((source) && (destination)) { try { var sourceInfo:XML = describeType(source); var prop:XML; for each(prop in sourceInfo.variable) { if(destination.hasOwnProperty(prop.@name)) { destination[prop.@name] = source[prop.@name]; } } for each(prop in sourceInfo.accessor) { if(prop.@access == "readwrite") { if(destination.hasOwnProperty(prop.@name)) { destination[prop.@name] = source[prop.@name]; } } } } catch (err:Object) { ; } } } }}