es6--解構賦值交換變數值
阿新 • • 發佈:2021-05-31
關於解構賦值的相關知識具體可以參考:es6--解構賦值
示例1:
<script>
let x = 1;
let y = 2;
//原來的方法
// let tmp = x;
// x = y;
// y = tmp;
// console.log(x, y);
//現在的方法
[x, y] = [y, x];
[x, y] = [2, 1];
console.log(x, y);
</script>
示例2:
<script> let x = 10; let y = 20; let z = 30; [x, y, z] = [z, x, y]; [x, y, z] = [30, 10, 20]; console.log(x, y, z); </script>