[b,a] =[a,b] 和 const [b,a]=[a,b] 有什么区别

What's the difference of between [b,a] =[a,b] and const [b,a]=[a,b]

在析构中,这两个代码的结果确实不同。我不知道为什么。

提示说 const [b,a] = [a,b] 将导致 a,b 的值为 undefined(从左到右的简单赋值规则)。我不明白为什么会这样。

let a = 8, b = 6;
(() => {
    [b,a]=[a,b];

})();
console.log(a); // should be 6
console.log(b); // should be 8

结果改变了,但是当附加 const 时,值没有切换。

The hint says const [b,a] = [a,b] will result in the value of a,b as undefined(simple assignment rule left to right). I can't understand why it happens.

不会。如果 FreeCodeCamp 说是,那就错了。

如果你在 [b,a] = [a,b] 之前添加了 const,你会得到一个 ReferenceError 因为你会 shadowing 外部 ab 与内部的,并在初始化之前尝试使用内部的:

let a = 8, b = 6;
(() => {
    const [b,a]=[a,b];

})();
console.log(a); // should be 6
console.log(b); // should be 8

如果他们的意思是在初始声明中使用 const 而不是 let,那么 不会产生他们列出的效果。相反,你会得到一个 TypeError 因为你试图分配给一个常量:

const a = 8, b = 6;
(() => {
    [b,a]=[a,b];

})();
console.log(a); // should be 6
console.log(b); // should be 8