簡體   English   中英

節點中數組解構的奇怪行為

[英]Strange behavior with Array deconstruction in Node

我遇到了以下情況引發undefined錯誤的情況:

...
(str) => {
  const [first, second] = str.split('.');
  // throws an undefined error
  // (can't find a .next of undefined? -- this may not be related)
}
...

因此,我轉換為以下代碼,並且可以正常工作:

...
(str) => {
  const temp = str.split('.');
  const first = temp[0];
  const second = temp[1];
}
...

奇怪的是,如果我將const轉換為let ,那么它將起作用:

...
(str) => {
  let temp = str.split('.');
  let [first, second] = temp;
}
...

節點v6.9.1

這很好

在節點上測試:

  • 6.0.0
  • 6.9.0
  • 6.9.1
  • 6.9.5
  • 6.11.1
  • 7.0.0
  • 7.10.1
  • 8.0.0
  • 8.1.4

請參閱GitHub上的項目:

查看Travis上的測試結果:

原始海報多次被要求提供顯示此行為的實際數據示例,但未能這樣做。 我和其他人特別要求提供將const更改為let時發生該錯誤和不發生該錯誤的Node版本和輸入示例。 由於沒有提供這樣的示例,因此我認為這個問題具有誤導性,並且它所描述的現象實際上並未發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM