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();

http://www.kirupa.com/forum/showpost.php?p=2230759&postcount=4

... 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.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <h3> <h4>
  • Lines and paragraphs break automatically.
  • HTML tags will be transformed to conform to HTML standards.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
9 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.