Copying objects
If you want to create a copy of an object, you can't simply use the assignment operator (=), because it will only perform the copy of the references.
Example
var obj1:Object = {firstName:"John", lastName:"Doe", age:35};
var obj2:Object = obj1;
At the end of an assignment operation you will have two references that point to the same memory location.
Any modification on the first object will be reflected on the other.
The Flex framework contains the ObjectUtil class, which has a copy static method. That method allows you to create deep copies of an object.
Example
var obj1:Object = {firstName:"John", lastName:"Doe", age:35};
var obj3:Object = ObjectUtil.copy(obj1);
Obj1 and obj3 are different references that point to different memory locations. Any modification on obj1 won't be reflected on obj3.
The copy method works well with objects of class Object, but it throws an error when you try to copy, for example, custom classes.
Example
var pers1:Person = new Person();
pers1.firstName = "Lisa"; pers1.lastName = "Redcliff"; pers1.age = 25;
var pers2:Person = ObjectUtil.copy(pers1) as Person;
trace("pers1 last name: " + pers1.lastName + " pers2 last name: " + pers2.lastName);
The trace statement throws an error. This happens because the copy method internally converts the source object in a ByteArray one, duplicates it and then converts the duplicated ByteArray in the destination object.
In other words, a binary AMF serialization/deserialization occurs. If you use custom classes, the Flash Player serializes correctly the source object, but doesn't know how to deserialize according to the specified cast (Person).
For this reason you have to register your custom class in order to give to the player the informations on how to perform the deserialization.
This can be done using the registerClassAlias method, which accepts 2 arguments:
- the fully qualified class name of the object to copy, which can be retrieved using flash.utils.getQualifiedClassName(sourceObj)
- the Class definition of the object, which can be retrieved using flash.utils.getDefinitionByName(className);</div>
After the class registration, the copy method can execute correctly.
Example
var obj1:Object = {firstName:"John", lastName:"Doe", age:35};
var obj2:Object = obj1;
At the end of an assignment operation you will have two references that point to the same memory location.
Any modification on the first object will be reflected on the other.
The Flex framework contains the ObjectUtil class, which has a copy static method. That method allows you to create deep copies of an object.
Example
var obj1:Object = {firstName:"John", lastName:"Doe", age:35};
var obj3:Object = ObjectUtil.copy(obj1);
Obj1 and obj3 are different references that point to different memory locations. Any modification on obj1 won't be reflected on obj3.
The copy method works well with objects of class Object, but it throws an error when you try to copy, for example, custom classes.
Example
var pers1:Person = new Person();
pers1.firstName = "Lisa"; pers1.lastName = "Redcliff"; pers1.age = 25;
var pers2:Person = ObjectUtil.copy(pers1) as Person;
trace("pers1 last name: " + pers1.lastName + " pers2 last name: " + pers2.lastName);
The trace statement throws an error. This happens because the copy method internally converts the source object in a ByteArray one, duplicates it and then converts the duplicated ByteArray in the destination object.
In other words, a binary AMF serialization/deserialization occurs. If you use custom classes, the Flash Player serializes correctly the source object, but doesn't know how to deserialize according to the specified cast (Person).
For this reason you have to register your custom class in order to give to the player the informations on how to perform the deserialization.
This can be done using the registerClassAlias method, which accepts 2 arguments:
- the fully qualified class name of the object to copy, which can be retrieved using flash.utils.getQualifiedClassName(sourceObj)
- the Class definition of the object, which can be retrieved using flash.utils.getDefinitionByName(className);</div>
After the class registration, the copy method can execute correctly.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onComplete()">
<mx:Script>
<![CDATA[
import flash.utils.getDefinitionByName;
import flash.net.registerClassAlias;
import flash.utils.getQualifiedClassName;
import com.Person;
import mx.utils.ObjectUtil;
protected function onComplete():void
{
/*-------------------------------------
Case 1: assigning object 1 to object 2
---------------------------------------*/
var obj1:Object = {firstName:"John", lastName:"Doe", age:35};
// The assignment acts on the references: obj1 and obj2 are
// 2 references that point to the same memory location
var obj2:Object = obj1;
// Modifying the age attribute of obj1, we affect also obj2
obj1.age = 45;
// traces "obj1 age: 45 obj2 age: 45"
trace("obj1 age: " + obj1.age + " obj2 age: " + obj2.age);
/*-------------------------------------
Case 2: using the copy method
---------------------------------------*/
// With the copy method the object memory location is copied,
// obj1 and obj3 references point to different memory locations
var obj3:Object = ObjectUtil.copy(obj1);
obj1.firstName = "Ken";
// traces "obj1 first name: Ken obj2 first name: John"
trace("obj1 first name: " + obj1.firstName + " obj3 first name: " + obj3.firstName);
var pers1:Person = new Person();
pers1.firstName = "Lisa";
pers1.lastName = "Redcliff";
pers1.age = 25;
// 1.
//var pers2:Person = ObjectUtil.copy(pers1) as Person;
// 2.
var pers2:Person = copyObject(pers1) as Person;
// Statement 1
// ------------
// throws an error
//
// Statement 2
// ------------
// traces "pers1 last name: Redcliff pers2 last name: Redcliff"
trace("pers1 last name: " + pers1.lastName + " pers2 last name: " + pers2.lastName);
}
/*
The copyObject method calls the copy function only when
the class is registered, so the ByteArray deserialization
can be done correctly.
*/
public function copyObject(sourceObj:Object):Object
{
var className:String = getQualifiedClassName(sourceObj);
registerClassAlias(className, getDefinitionByName(className) as Class);
return ObjectUtil.copy(sourceObj);
}
]]>
</mx:Script>
</mx:Application>




