From: VK on
On Jun 16, 12:39 pm, VK <schools_r...(a)yahoo.com> wrote:
> 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);

The other option is to not use stay-alone primitives but object
properties as objects being passed to functions by reference values:

var foo = {'a' : 10};
var bar = {'a' : 20};

swap(foo, bar, 'a');

window.alert(foo.a + ' ' + bar.a);

function swap(obj1, obj2, prop) {
var tmp = obj1[prop];
obj1[prop] = obj2[prop];
obj2[prop] = tmp;
}

Whichever is more (dis)convenient depends on your actual project.


From: Asen Bozhilov on
VK wrote:

> var foo = {'a' : 10};
> var bar = {'a' : 20};
>
> swap(foo, bar, 'a');
>
> window.alert(foo.a + ' ' + bar.a);
>
> function swap(obj1, obj2, prop) {
>  var tmp = obj1[prop];
>  obj1[prop] = obj2[prop];
>  obj2[prop] = tmp;
>
> }

The whole point of thread is useless and as Thomas pointed out, he
does not need function/method especially "generic". He can use
standard approach, for example:

var foo = 10,
bar = 20;

foo = [bar, bar = foo][0];

From: Thomas 'PointedEars' Lahn on
VK wrote:

> VK wrote:
>> 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);
>
> The other option is to not use stay-alone primitives but object
> properties as objects being passed to functions by reference values:

Which is neither generic nor the best way to do it. Thanks for playing.

--
PointedEars
From: VK on
On Jun 16, 3:28 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote:
> The whole point of thread is useless and as Thomas pointed out, he
> does not need function/method especially "generic". He can use
> standard approach, for example:
>
> var foo = 10,
>     bar = 20;
>
> foo = [bar, bar = foo][0];

Let's not reproduce the infamous "The Fox and the Grapes"
situation ;-)
http://en.wikipedia.org/wiki/The_Fox_and_the_Grapes
so like if something cannot be done in that way then it doesn't need
to be done in that way. There is nothing particularly strange in the
necessity to swap some values. If it needs to be done more than once
in the same program then there is nothing strange in the desire to
make it as a subroutine. The fact that it is not possible for stay
alone primitives in JavaScript is far of being obvious for people
coming from some other programming platforms. Yes, the task can be
still accomplished in a number of ways like yours or mine. No, it
cannot be accomplished in a subroutine for stay alone primitives.

From: Thomas 'PointedEars' Lahn on
Asen Bozhilov wrote:

> The whole point of thread is useless and as Thomas pointed out, he
> does not need function/method especially "generic". He can use
> standard approach, for example:
>
> var foo = 10,
> bar = 20;
>
> foo = [bar, bar = foo][0];

That is generic, but the non-obvious code style aside, I would not want to
rely on that it works. The Specifications imply that the /ElementList/ is
evaluated from left to right [11.1.4], but does it follow that all
implementations must do that?

I would also be positively surprised if the creation of an Array instance, a
property lookup and two assignments was more efficient than a variable
declaration and three assignments. That said, I would like very much other
implementations to adopt the desctructuring assignment and ES 6 "Harmony" to
specify it.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)