Variable assignment by value rather than by reference in ActionScript 3 for Arrays

Something that has bugged me a few times, but I needed to figure out this time, as a Flash beginner, is that because everything is an object (and thus an instance of a class, including primatives) you more often than not (unless using primitives/statics) bind everything because you’re always passing variables by reference.

Coming from a Basic/C/Perl/PHP background where you can pass things by either value or reference it took a while for me to figure out how to just copy an array! Since $array2 = $array1; in PHP for example would copy array 1 to array 2, in ActionScript it will bind array 2 to array 1, and weirdly there seems to way (I can find yet) to pass objects by value. But having found the concat function:

array2 = array1.concat();

… you can create a copy of array 1 by returning a new Array object with identical contents. Having seen some deep copy examples I’m assuming that most objects come supplied with copy methods so I don’t think it will be a problem in future, but something that newbies to the language may need to be aware of.

Add comment