From: proxygeek on
I could not find any worthwhile discussion on implementing a generic
swap(a,b) function in Javascript.
Please point me to a discussion/link if it's already covered there.

Problem:
I need a method swap(a,b) which I can call with two variables as
params and it would swap the values of the two vars.

<snip>
....
....
function swap(a,b){ // This would NOT work as
intended
t = a;
a = b;
b = t;
}
....
....
var x = 10, y = 20;
swap(x,y);
alert("x = " + x + ", y = " + y); // should give x =
20, y = 10
....
</snip>

Above implementation would not work as here swap() does not know a
thing about x or y.

What would be the best solution for this?

Regards,
proxygeek
From: Thomas 'PointedEars' Lahn on
proxygeek wrote:

> I could not find any worthwhile discussion on implementing a generic
> swap(a,b) function in Javascript.

Because there isn't one.

> Problem:
> I need a method swap(a,b) which I can call with two variables as
> params and it would swap the values of the two vars.

No, you don't.

> <snip>
> ....
> ....
> function swap(a,b){ // This would NOT work as
> intended

Original code should be posted so that it is readable and executable when
wrapped to the customary 72 to 80 characters per line. Always use multi-
line pre-comments, not single-line post-comments, for documentation
comments.

> t = a;

You "forgot" to declare `t', so it leaks to the outer scope.

> a = b;
> b = t;
> }
> ....
> ....
> var x = 10, y = 20;
> swap(x,y);
> alert("x = " + x + ", y = " + y); // should give x =

window.alert(…);

> 20, y = 10

See above

> ....
> </snip>
>
> Above implementation would not work as here swap() does not know a
> thing about x or y.

Congratulations, you have discovered call-by-value.

> What would be the best solution for this?

Do not use a function/method.

JavaScript 1.7+:

/* Destructuring assignment */
[y, x] = [x, y];

Other versions/implementations: as in swap()

See also <http://PointedEars.de/es-matrix>


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)
From: VK on
On Jun 16, 12:13 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> JavaScript 1.7+:
>
>   /* Destructuring assignment */
>   [y, x] = [x, y];

Not "JavaScript 1.7+" but Mozilla JavaScript 1.7+ thus Gecko platforms
thus 10%-20% of average visitors. IMO way below the level of an
acceptably universal solution.

> Other versions/implementations: as in swap()

? Do you imply that other platforms do implement call-by-reference for
primitives? It is not true.

To OP: to my surprise this question seems never was answered properly
in this newsgroup. The only relevant discussion I found was from
2000:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/b72a4c3346057c66/e80dfd68d712c73c
and it doesn't contain any valuablу answer.

JavaScript as currently defined by ECMA-262 3rd ed. by design doesn't
allow primitives' swap in a separate function, because the function
gets such arguments by values, not by references.

If your code requires frequent primitive swaps, you may define an
intermediary var at the beginning so do not create it over and over
again and then:

var swap = null;
// ...
var x = 10, y = 20;
// ...
swap = x, x = y, y = swap;
window.alert("x = " + x + ", y = " + y);


From: Thomas 'PointedEars' Lahn on
VK wrote:

> Thomas 'PointedEars' Lahn wrote:
>> JavaScript 1.7+:
>>
>> /* Destructuring assignment */
>> [y, x] = [x, y];
>
> Not "JavaScript 1.7+" but Mozilla JavaScript 1.7+

"Mozilla" is superfluous when talking about "JavaScript". I have explicitly
pointed out the other implementations, so there can not be ambiguity.
Unless, of course, one *wants* to misunderstand in order to have a case for
bickering and fairytales.

> thus Gecko platforms

JavaScript is not limited to "Gecko platforms".

> thus 10%-20% of average visitors.

That depends on the country to begin with.

> IMO way below the level of an acceptably universal solution.

Your opinion is worthless as you don't know what you are talking about.

>> Other versions/implementations: as in swap()
>
> ? Do you imply that other platforms do implement call-by-reference for
> primitives?

No.

> It is not true.

You are jumping to conclusions.

> To OP: to my surprise this question seems never was answered properly
> in this newsgroup. The only relevant discussion I found was from
> 2000:

OP: Do not listen to, and do not feed the troll.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
From: VK on
On Jun 16, 12:50 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> "Mozilla" is superfluous when talking about "JavaScript".

Do you have links to documents proving Mozilla's exclusive rights to
JavaScript standardization or/and Mozilla's extensions
"standardization priority" ?

> > thus 10%-20% of average visitors.
> > IMO way below the level of an acceptably universal solution.
>
> Your opinion is worthless as you don't know what you are talking about.

I am talking about the destructuring assignment JavaScript extension
and its support across used browsers. What are you talking about
remains a mystery to the side readers.

> > ? Do you imply that other platforms do implement call-by-reference for
> > primitives?
>
> No.

That's good to know.

> OP: Do not listen to, and do not feed the troll.

You worthless trolling is annoying. Make your *working* version of
swap() for an ECMA-262 3rd ed. compliant platforms other than newest
Fx or just stop the tread pollution.