How to Clone Objects in AS3

admin  

Cloning objects in as3 is a useful method that is not implemented directly in language but that can be implemented in a few lines of code, easier than in other languages like java.

Before starting we should mention that there are 2 way of cloning object: shallow cloning and deep cloning. Shallow cloning assumes that an cloned object is identical with the original object and keeps the references to the same objects where the original object points too. If for example we have a tree object that contains a variable to a park object, the shallow copy of the tree object will contain variables to the same park object.

That sounds good, but what should we do when we have a car object? If we clone it the cloned car will point to the same wheels objects as the original car. That sound awkward. When we clone the car object we should go through the wheels and clone each one. The cloned car object will point to 4 new cloned wheels. That’s called deep cloning.

In practice shallow cloning might generate such problems, but deep cloning has too many drawbacks(especially performance drawbacks). Most probably shallow copy would be good for most of the situations, or a combination of those 2. Blind deep cloning is probably rarely used.

In this post will show a few simple methods for cloning objects in action script 3.0. We are going to make use of a simple mechanism provided by as3 that allows us to read the properties of an object:

Objects in AS3 are treated in a similar way with arrays: while the arrays have objects stored at numeric indexes, the objects stores objects at some properties which can be considered some String indexes. For example to iterate through the elements of an array is similar to iterate through the properties values of an object:

for each(var value:Object in arrayA)
     trace('value:', value);

for each(var value:Object in objectA)
     trace('value:', value);

For a simple object if we use ‘for’ instead of ‘for each’ the loop will iterate through the property names instead of property values; the following piece of code reads the properties names and displays them in the console along with the values:

The next step for cloning an object is to read the data from one object and to put it in a second one:

var objectA:Object = {propery1:'string1', propery2:0.4};
var objectB:Object = new Object();

for (var property:String in objectA)
     objectB[property] = objectA[property];

Now we know how to copy the properties from one object to a fresh one. If we want to make a generic method to clone objects we have to create instances of the same type for the object we want to clone. In a previous post we’ve seen how to Get The Class Of An Object In AS3 and we are going to use it:

var cls:Class = getDefinitionByName(getQualifiedClassName(objectA)) as Class;
var objectB:Object = new cls(); //create an instance of the same class as objectA

for (var property:String in objectA)
     objectB[property] = objectA[property];

Sometimes we don’t need to blindly copy all the properties of an object and we know exactly what properties we need to copy to a new object so we simply specify them in an array:

var objectA:Object = { propery1:'string1',
                propery2:'we do not need this one', 
                property3:0.54};
var objectB:Object = new Object();

for each (var prop:String in ['propery1', 'antiAliasType'])
{
    otf[prop] = tf[prop];
}

In a future post we are going to analyze further how deal with read only properties, how to make recursive functions for deep cloning and how to mix deep cloning and shallow cloning.