Permalink
Browse files

Various ways to swap two variables

  • Loading branch information...
1 parent 5c98500 commit b070fc16d245b43aa1dacce166718d156a846cd0 @isaacs committed Jul 5, 2010
Showing with 22 additions and 0 deletions.
  1. +22 −0 examples/var-swap.js
View
@@ -0,0 +1,22 @@
+
+var foo = 1
+ , bar = 2
+
+function tmpvar () {
+ var _ = foo
+ foo = bar
+ bar = _
+}
+function arrswap () {
+ foo = [bar, bar = foo][0]
+}
+function fnswap () {
+ (function(x,y){foo=x;bar=y})(bar,foo)
+}
+
+exports.compare =
+ { tmpvar: tmpvar
+ , arrswap: arrswap
+ , fnswap: fnswap
+ , "nil function":function () {}
+ }

0 comments on commit b070fc1

Please sign in to comment.