15 May 2011

AS 3.0 Copy or Clone a Object

You have your own object instance and you want to copy or clone an exact copy of it?

Simple, you can use Flex built-in ObjectUtil copy function as follows:

import mx.utils.ObjectUtil;
var obj:Object = new Object();
obj.content = "hello";
var objCopy:Object = ObjectUtil.copy(obj);

In case you want it in Actionscript 3.0 instead of Flex in-built capability:-
private function clone(source:Object):* 
{
var copier:ByteArray = new ByteArray();
copier.writeObject(source);
copier.position = 0;
return(copier.readObject());
}

0 comments:

Post a Comment