方法一:
使用第三方变量temp交换
let x = 1;
let y = 2;
let temp = x;
x = y;
y = temp;
console.log(x, y); // 2 1
方法二:
使用同构解构交换
var arr = [1, 2];
var [a, b] = arr; //解构
arr = [b, a]; //同构
[a, b] = [b, a]
console.log(a, b); //2 1
方法一:
使用第三方变量temp交换
let x = 1;
let y = 2;
let temp = x;
x = y;
y = temp;
console.log(x, y); // 2 1
方法二:
使用同构解构交换
var arr = [1, 2];
var [a, b] = arr; //解构
arr = [b, a]; //同构
[a, b] = [b, a]
console.log(a, b); //2 1