第一种:常规置换,借助中间变量
let a = 1, b = 2, c;
c = a;
a = b;
b = c;
//==> a = 2, b = 1
第二种:借助Object
let a = 1, b = 2;
let obj = {
a: b,
b: a,
}
a = obj.a; b = obj.b;
//==> a = 2, b = 1
第三种:ES6的赋值解构
let a = 1, b = 2;
[a, b] = [b, a];
//==> a = 2, b = 1
网友评论