Copying or cloning arrays
Posted in Flash on November 20th, 2009 by Thomas H – 1 CommentObviously, this is something you already know, but if like me, you didn’t know what happens when you copy arrays this might help.
var a:Array = [new Object(), new Object(), new Object()]; var b:Array; //Create a new reference to the array b = a; //Create a new array but have the same reference to its objects ( shallow copy ) b = a.slice(); b = a.concat(); //faster //A way to actually clone an array and get new instance of the array and new instances of its objects function clone(source:Object):* // function for deep copy { var myBA:ByteArray = new ByteArray(); myBA.writeObject(source); myBA.position = 0; return(myBA.readObject()); } b = clone(a);
Don’t know if this is necessary needed for arrays containing numbers or string values, but for arrays with objects it is.
