<p>Actionscript 3.0 (and I assume Javascript and ECMAScript in general) lacks pass-by-reference for native types like ints. As a result I'm finding getting values back from a function really clunky.  What's the normal pattern to work around this?  </p>

<p>For example, is there a clean way to implement <em>swap( intA, intB )</em> in Actionscript?</p>

[{'answer_id': 49109, 'author': 'Nick Retallack', 'author_id': 2653, 'author_profile': 'https://Stackoverflow.com/users/2653', 'pm_score': -1, 'selected': False, 'text': '<p>If ActionScript works like Javascript,</p>\n\n<pre><code>[a,b] = [b,a]\n</code></pre>\n'}, {'answer_id': 50755, 'author': 'hasseg', 'author_id': 4111, 'author_profile': 'https://Stackoverflow.com/users/4111', 'pm_score': 4, 'selected': True, 'text': '<p>I Believe the best you can do is pass a container object as an argument to a function and change the values of some properties in that object:</p>\n\n<pre><code>function swapAB(aValuesContainer:Object):void\n{\n    if (!(aValuesContainer.hasOwnProperty("a") &amp;&amp; aValuesContainer.hasOwnProperty("b")))\n        throw new ArgumentError("aValuesContainer must have properties a and b");\n\n    var tempValue:int = aValuesContainer["a"];\n    aValuesContainer["a"] = aValuesContainer["b"];\n    aValuesContainer["b"] = tempValue;\n}\nvar ints:Object = {a:13, b:25};\nswapAB(ints);\n</code></pre>\n'}, {'answer_id': 50990, 'author': 'imaginaryboy', 'author_id': 2508, 'author_profile': 'https://Stackoverflow.com/users/2508', 'pm_score': 2, 'selected': False, 'text': '<p>I suppose an alternative would be somewhere defining this sort of thing ...</p>\n\n<pre><code>public class Reference {\n    public var value:*;\n}\n</code></pre>\n\n<p>Then use functions that take some number of Reference arguments to act as "pointers" if you\'re really just looking for "out" parameters and either initialize them on the way in or not and your swap would become:</p>\n\n<pre><code>function swap(Reference a, Reference b) {\n    var tmp:* = a.value;\n    a.value = b.value;\n    b.value = tmp;\n}\n</code></pre>\n\n<p>And you could always go nuts and define specific IntReference, StringReference, etc.</p>\n'}, {'answer_id': 50992, 'author': 'Andres', 'author_id': 1815, 'author_profile': 'https://Stackoverflow.com/users/1815', 'pm_score': 2, 'selected': False, 'text': '<p>You could also use a wrapper instead of int:</p>\n\n<pre><code>public class Integer\n{\n    public var value:int;\n\n    public function Integer(value:int)\n    {\n        this.value = value;\n    }\n}\n</code></pre>\n\n<p>Of course, this would be more useful if you could use operator overloading...</p>\n'}, {'answer_id': 51315, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 2, 'selected': False, 'text': "<p>This is nitpicking, but <code>int</code>, <code>String</code>, <code>Number</code> and the others <em>are</em> passed by reference, it's just that they are immutable. Of course, the effect is the same as if they were passed by value.</p>\n"}, {'answer_id': 657222, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>It is annoying. But if you use different idioms than in e.g. C#, you can get reasonable-quality results. If you need to pass a lot of parameters back and forth, pass in an object filled with the needed data, and change the object's parameters when you return. The Object class is for just this sort of thing.</p>\n\n<p>If you just need to return a bunch of data, return an Object. This is more in keeping with the ECMAScript style than pass-by-ref semantics.</p>\n"}, {'answer_id': 1313114, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Destructuring assignment (e.g. [a,b] = [b,a]) isn't defined in the ECMA-262 3 specification, and it's not implemented in JavaScript 1.5, which is the version equivalent to the JScript implementation in IE. I've seen this syntax in the AS4 specifications preview though, and I believe it's part of JavaScript 1.7.</p>\n"}, {'answer_id': 2434629, 'author': 'acgeek', 'author_id': 292536, 'author_profile': 'https://Stackoverflow.com/users/292536', 'pm_score': 2, 'selected': False, 'text': "<p>Just look at some Java code.  Java has had the convention that reference types are passed by reference and primitive types are passed by value since it's inception.  It's a very good model in many ways.</p>\n\n<p>But talking about swap, the best and easiest way to do a swap in Java/AS3 is with the following three lines:</p>\n\n<pre>\nvar temp:int = array[i];\narray[j] = array[i];\narray[i] = temp;\n</pre>\n\n<p>Theres not really any reason to use a function to do a simple swap, when you can do it faster with just 3 lines.</p>\n"}]
['https://Stackoverflow.com/questions/49107', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4969/']