10种JavaScript交换值的方法
来源 | https://juejin.im/post/6873611999238946829
1、使用临时变量
function swapWithTemp(num1, num2) {
console.log(num1, num2)
let temp = num1
num1 = num2
num2 = temp
console.log(num1, num2)
}
swapWithTemp(66.66, 8.88)
2、使用算术运算符+
和-
function swapWithPlusMinus(num1, num2){
console.log(num1, num2)
num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2
console.log(num1, num2)
}
swapWithPlusMinus(66, 8)
num2 = num1-num2
,同理,第一个数要换成第二个数的值,就是总的和减去第一个数的值,现在第一个数已经是赋值给第二个数,所以直接减去第二数的值即可,也就是 num1 = num1-num2
。function swapWithPlusMinusShort(num1, num2){
console.log(num1, num2)
num2 = num1 + (num1 = num2) - num2
console.log(num1, num2)
}
(num1 = num2)
,这步,我们让 num1
等于 num2
了,并且返回是 num2
的值,此时 num1
值已交换。接着就用 num1
加上 (num1 = num2)
返回的值,也就是 num1 + num2
求和,然后思路就和上面分析的一样了 。function swapWithPlusMinusShort(num1, num2){
console.log(num1, num2)
num2 = num1 + (num1 = num2) - num2
console.log(num1, num2)
}
swapWithPlusMinusShort(2,3.1)
3、仅使用+或-运算符
function swapWithPlus(num1, num2){
console.log(num1, num2)
num2 = num1 + (num1=num2, 0)
console.log(num1, num2)
}
swapWithPlus(2.3,3.4)
()
中,我们将num1
分配给num2
,旁边的0
是返回值。简而言之,第4
行看起来是这样的 :num2 = num1 + 0 => num2 = num1
4、 使用算术运算符*和/
function swapWithMulDiv(num1, num2){
console.log(num1, num2)
num1 = num1*num2
num2 = num1/num2
num1 = num1/num2
console.log(num1, num2)
}
swapWithMulDiv(2.3,3.4)
swapWithMulDiv(2.34,0)
// 2.34 0
// NaN NaN
function swapWithMulDiv(num1, num2){
console.log(num1, num2)
num1 = num1*num2
num2 = num1/num2
num1 = num1/num2
console.log(num1, num2)
}
swapWithMulDiv(2.34,Infinity)
// 2.34 Infinity
// NaN NaN
function swapWithMulDiv(num1, num2){
console.log(num1, num2)
num1 = num1*num2
num2 = num1/num2
num1 = num1/num2
console.log(num1, num2)
}
swapWithMulDiv(2.34,-Infinity)
function swapWithMulDivShort(num1, num2){
console.log(num1, num2)
num1 = num1*num2
num2 = num1*(num1=num2)/num2
num1 = num1/num2
console.log(num1, num2)
}
swapWithMulDivShort(2.3,3.4)
5、仅使用*或/运算符
num2 = num1 * 1 => num2 = num1
6、使用按位异或
0:
function swapWithXOR(num1, num2){
console.log(num1, num2)
num1 = num1^num2;
num2 = num1^num2;
num1 = num1^num2;
console.log(num1, num2)
}
swapWithXOR(10,1)
num1 = num1 ^ num2 = 1010 ^ 0001 = 1011
num2 = num1 ^ num2 = 1011 ^ 0001 => 1010 => 10
num1 = num1 ^ num2 = 1011 ^ 1010 => 0001 => 1
function swapWithXOR(num1, num2){
console.log(num1, num2)
num1 = num1^num2;
num2 = num1^num2;
num1 = num1^num2;
console.log(num1, num2)
}
swapWithXOR(2.34,3.45)
// 2.34 3.45
// 3 2
function swapWithXOR(num1, num2){
console.log(num1, num2)
num1 = num1^num2;
num2 = num1^num2;
num1 = num1^num2;
console.log(num1, num2)
}
swapWithXOR(-Infinity,Infinity)
// -Infinity Infinity
// 0 0
function swapWithXNOR(num1, num2){
console.log(num1, num2)
num1 = ~(num1^num2)
num2 = ~(num1^num2)
num1 = ~(num1^num2)
console.log(num1, num2)
}
swapWithXNOR(10,1)
num1 = ~(num1 ^ num2) => ~(1010 ^ 1011)=> ~(1011) => ~11 => -12
-12 => 1100 => 0011 + 1 => 0100
num2 = ~(num1 ^ num2) => ~(0100 ^ 0001) => ~(0101) => ~5 => -6
-6 => 0110 => 1001 + 1 => 1010 => 10
num1 = ~(num1 ^ num2) => ~(0100^ 1010) => ~(1110) => ~14 => -15
-15 => 1111 => 0000 + 1 => 0001 => 1
function swapWithXNOR(num1, num2){
console.log(num1, num2)
num1 = ~(num1^num2)
num2 = ~(num1^num2)
num1 = ~(num1^num2)
console.log(num1, num2)
}
swapWithXNOR(2.3,4.5)
// 2.3 4.5
// 4 2
8、在数组中赋值
function swapWithArray(num1, num2){
console.log(num1, num2)
num2 = [num1, num1 = num2][0]
console.log(num1, num2)
}
swapWithArray(2.3,Infinity)
// 2.3 Infinity
// Infinity 2.3
9、使用解构表达式
let num1 = 23.45
let num2 = 45.67
console.log(num1,num2)
[num1,num2] = [num2,num1]
console.log(num1,num2)
10、使用立即调用的函数表达式(IIFE)
function swapWithIIFE(num1,num2){
console.log(num1,num2)
num1 = (function (num2){ return num2; })(num2, num2=num1)
console.log(num1,num2)
}
swapWithIIFE(2.3,3.4)
评论