js --- swap two values

This article summarizes the seven ways to exchange a and b of the variable value 

var a = 123; 
var b = 456; exchange program a variable value 
most easiest way is to use a temporary variable, but the use of temporary variables method it is too low a 
var T; 
T = a; 
a = b; 
b = T; 
first storing the value of a temporary variable, and then assigned to a b, and finally out a value of the temporary variable is assigned to b, this approach is the most basic of the exchange value of the variable program two 
following scenario will not be temporary variables, I summed up what, in fact, do not use temporary variables idea is to let one of the variables a and b are turned into a relationship the values as to change the value of another variable, the final value of the original variable change modified 
such that 
a + = b; 
b = a - b; 
a - = b; 
let a first converted into the a and b 'and' (also It can be replaced with the difference between a and b, the same), 'and' b ingenious been subtracted a value to the variable b, then by imparting a 'and' a variety of subtracting to the value of b, or below variant (differential form) 
a - = B; 
B = a + B; 
a = B - a; 
but felt form and more readily appreciated from the exchange side variable value Three 
this approach may not be aware for the first time for students to learn JavaScript, JavaScript because we rarely use in place operation, which is what I learned in the past when looking algorithm contest of the book, to be exchanged by the underlying bit computing variable values, but also to the evolution of the above scheme 
a ^ = b;





b ^ = A; 
A ^ = b; 
look at it, C ++ or even a ^ = b ^ = a ^ = b to complete the task, but I find that JavaScript can not 
, but we can do 

a = (b ^ = a ^ = b) ^ a; switching variable value program IV 
put into a a first object that holds the key to be in exchange for the last assignment get 
a = {a: B, B: a}; 
B = ab &; 
a = aa; exchange scheme five variable values 
and the above methods like, but the object into an array 
a = [a, B]; 
B = a [0]; 
a = a [. 1]; exchange scheme six variable value 
this approach is very clever, I did not want to come out, to come out of the people must be the great God, unless he is asked dream out, simple and crude line of code to exchange a and b values of the variables 
a = [b, b = a ] [ 0]; 
the operator precedence, a = b is first performed, in this case b the value of a variable directly, and a step so that the array index value b is obtained (simply can not be worse) switching the variable value of the seventh aspect 
final my solution is to use a deconstruction ES6 assignment syntax, it allows us to extract the value of arrays and objects, variable assignment, but I am testing the chrome browser has been achieved [a, b] = [b , a]; 
see deconstruction assignment syntax Let's exchange value of the variable becomes super simple, this deconstruction assigned a confession is not a lot of focus today talking about grammar and if, in the future will not talk about it now and then sum










  

Guess you like

Origin www.cnblogs.com/jcxfighting/p/12345380.html