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>


And the UtilFunctions.as file:


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) {
;
}
}
}
}
}


文章摘自:
http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值