For the cloning of array in ActionScript there are no built in methods. Thus the cloning is done basically using two methods namely clone( ) or slice( ). For the shallow copy of the array these methods are used without any arguments. If in the original array the elements are the objects then during shallow copy only references of the objects are copied instead of the actual objects. If any changes are made in the object then it will be reflected in both the arrays. For example :
var a:Array = new Array(1,2,3);
var b:Array = a;
In the above code it looks like we are assigning the value of a to b. The above code will not copy the elements of a to b because in ActionScript there are no copy constructor for array. So , if you really want to copy any array then you need to use any of the methods mentioned above.
var a:Array = new Array (1,2,3);
var b:Array=a.slice();
If you want the clone the array that only has numbers and string then in that case the slice( ) should be used and if you have multi-dimensional array then in that case clone( ) should be used for the cloning.
import flash.utils.ByteArray;
function clone(source:Object):*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}
0 Comment(s)