将值重新分配给使用解构创建的变量的正确方法? (数组解构)

Correct way to reassign value to variables that were created using destructuring? (Array destructuring)

我刚开始学习解构 (n00b),并且一直在学习前端大师课程。呈现方式如下:

let [firstVar, secondVar] = ["firstValue", "secondValue"]

这是有道理的。我可以(并且已经)验证 firstVar 获得值 "firstValue" 并且 secondVar 获得值 "secondValue".

令人困惑的是,我似乎无法重新分配存储在这些变量中的值。我同时使用了 constlet(以上面显示的方式)并尝试重新分配变量。在我使用 const 声明变量的地方没有抛出错误。如果我在浏览器控制台中玩耍,我会得到类似这样的信息:

const [firstThing, secondThing] = ["first value", "second value"] 
console.log(firstThing) // expected output: first value
firstThing = "some new thing" 
console.log(firstThing) // expected output: first value

FireFox 控制台中此行为的屏幕截图(我导航到 about:blank 以确保内存中没有旧变量):

同样,没有抛出任何错误(我通常会在尝试重新分配给 const 变量时发生错误。

如果我执行以下操作,我会得到预期的行为:

[firstThing] = ["some new value"]
// TypeError: invalid assignment to const 'firstThing'

当使用 let 完成解构时,我能够(看似)使用类似的语法重新分配值(将变量和新值括在括号中)。

编辑: 人们表示,当 运行 浏览器中显示的代码时,他们无法重现问题。我最初并没有在 Node REPL 中使用它,但这样做会产生预期的行为(尝试重新分配给 const var 会引发错误,let 变量可以重新分配)。在这一点上,我很好奇为什么这种行为在我的浏览器中不一样。我使用的是 64.0.2 版。

为了弄清楚这一点,我浏览了以下内容:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

https://javascript.info/destructuring-assignment

两者似乎都没有涉及为使用解构创建的变量(或对象)重新赋值的问题。我接近它的方式在技术上是正确的吗?我也开始被介绍到垃圾收集等(我只有最简短的介绍)所以我想知道我正在使用的重新分配 "technique" 是否正在创建一堆没有得到的东西垃圾收集或创建奇怪的指针等可能会在以后导致问题。

no error is thrown (which I would normally expect when trying to reassign to a const variable.

我无法重现。当 运行 该代码时,我看到一个错误:"Uncaught TypeError: Assignment to constant variable."

const [firstThing, secondThing] = ["first value", "second value"] 
console.log(firstThing) // expected output: first value
firstThing = "some new thing" 
console.log(firstThing) // expected output: first value

如果我使用 let,它允许我重新分配。

let [firstThing, secondThing] = ["first value", "second value"] 
console.log(firstThing) // expected output: first value
firstThing = "some new thing" 
console.log(firstThing) // expected output: first value

也无法在 firefox 的控制台中重现:

诊断您的环境与我们的环境有何不同有点困难。也许是不同的浏览器版本,也许是插件,谁知道呢。无论如何,您发布的代码只是替换为 let 而不是 const 是重新分配变量的正确方法。

这在 FireFox 中似乎有些奇怪。我刚刚更新到 65.0,此行为不再发生。