无法在交换过程中访问未初始化的数组

huangapple go评论71阅读模式
英文:

Cannot access array before initilaisation during swap

问题

Ive come to something strange

我遇到了一些奇怪的事情

Im trying to swap the array value 0 with the array value 1

我试图将数组中的值0与数组中的值1互换

Ive found a previous post stating that this is possible

我找到了以前的帖子,其中说明这是可能的

[arr[0], arr[1]] = [arr[1], arr[0]];

所以我想要复制这个

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"]
[array_names[0], array_names[1]] = [array_names[1], array_names[0]]

However something strange happens

然而,发生了一些奇怪的事情

I get an error message stating that

我收到一个错误消息,其中指出

ReferenceError: Cannot access 'array_names' before initialization

引用错误:无法在初始化之前访问 'array_names'

I have checked and there isnt a typo anywhere

我已经检查过,没有任何拼写错误

Also yes I did initialize the array before doing any modification

而且是的,我在进行任何修改之前已经初始化了数组

英文:

Ive come to something strange

Im trying to swap the array value 0 with the array value 1

Ive found a previous post stating that this is possible

[arr[0], arr[1]] = [arr[1], arr[0]];

So I wanted to replicate that

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"]
[array_names[0], array_names[1]] = [array_names[1], array_names[0]]

However something strange happens

I get an error message stating that

ReferenceError: Cannot access 'array_names' before initialization

I have checked and there isnt a typo anywhere

Also yes I did initialize the array before doing any modificiation

答案1

得分: 3

你的代码完全正确,但由于缺少分号,解释器解析了你的代码不同的方式。

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"]
[array_names[0], array_names[1]] = [array_names[1], array_names[0]]

这个表达式被解释为

["Dave", "johson", ...][array_names[0], array_names[1]]

代码被解析为一行语句。

正确的答案如Shubhada所述:

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"]; // 终止语句
[array_names[0], array_names[1]] = [array_names[1], array_names[0]];
英文:

Your code is totally correct, but due to a missing semicolon, the interpreter parsed your code differently..

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"]
[array_names[0], array_names[1]] = [array_names[1], array_names[0]]

The expression was evaluated as

["Dave", "johson", ...][array_names[0], array_names[1]] 

The code is parsed as one line statement.

The right answer as stated by Shubhada is

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"]; //Terminate statement
[array_names[0], array_names[1]] = [array_names[1], array_names[0]];

答案2

得分: 2

JS实际上试图将您的代码解释为:

// 缩短代码以节省篇幅
let array_names = ["Dave", "johson"][array_names[0], array_names[1]] = [array_names[1], array_names[0]]

它试图访问数组["Dave", "johson"]的索引[array_names[0], array_names[1]],而在那个时候,array_names还没有被定义。

您可以(应该)使用分号来修复这个问题:

let array_names = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"];
[array_names[0], array_names[1]] = [array_names[1], array_names[0]];
console.log(array_names);
英文:

JS is actually trying to interpret your code as :

// reduced for brevity
let array_names = ["Dave", "johson"][array_names[0], array_names[1]] = [array_names[1], array_names[0]]

It's trying to access the index [array_names[0], array_names[1]] of the array ["Dave", "johson"] and at that time, array_names not yet defined.

You can (should) use semicolon to fix that :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let array_names = [&quot;Dave&quot;, &quot;johson&quot;, &quot;Lime&quot;, &quot;Key&quot;, &quot;Freeman&quot;, &quot;Chell&quot;, &quot;Shepart&quot;, &quot;Furious&quot;];
[array_names[0], array_names[1]] = [array_names[1], array_names[0]];
console.log(array_names);

<!-- end snippet -->

答案3

得分: 1

I try following

let array_names = [
  "Dave", "johson",
  "Lime", "Key",
  "Freeman", "Chell", 
  "Shepart", "Furious"
]

console.log(array_names)

array_names[0], array_names[1] = array_names[1], array_names[0]

console.log(array_names)

temp = array_names[0]
array_names[0] = array_names[1]
array_names[1] = temp

console.log(array_names)

and get the result

[
  'Dave',    'johson',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]
[
  'johson',  'Dave',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]
[
  'Dave',    'johson',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]

as in here. But with your code I obtain

[
  'Dave',    'johson',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]
[
  'Dave',    'johson',
  'Lime',    'Key',
  'Freeman', 'Chell',
  'Shepart', 'Furious'
]

where the elements are not swapped. I do not understand either.

英文:

I try following

let array_names = [
  &quot;Dave&quot;, &quot;johson&quot;,
  &quot;Lime&quot;, &quot;Key&quot;,
  &quot;Freeman&quot;, &quot;Chell&quot;, 
  &quot;Shepart&quot;, &quot;Furious&quot;
]

console.log(array_names)

array_names[0], array_names[1] = array_names[1], array_names[0]

console.log(array_names)

temp = array_names[0]
array_names[0] = array_names[1]
array_names[1] = temp

console.log(array_names)

and get the result

[
  &#39;Dave&#39;,    &#39;johson&#39;,
  &#39;Lime&#39;,    &#39;Key&#39;,
  &#39;Freeman&#39;, &#39;Chell&#39;,
  &#39;Shepart&#39;, &#39;Furious&#39;
]
[
  &#39;Dave&#39;,    &#39;johson&#39;,
  &#39;Lime&#39;,    &#39;Key&#39;,
  &#39;Freeman&#39;, &#39;Chell&#39;,
  &#39;Shepart&#39;, &#39;Furious&#39;
]
[
  &#39;johson&#39;,  &#39;Dave&#39;,
  &#39;Lime&#39;,    &#39;Key&#39;,
  &#39;Freeman&#39;, &#39;Chell&#39;,
  &#39;Shepart&#39;, &#39;Furious&#39;
]

as in here. But with your code I obtain

[
  &#39;Dave&#39;,    &#39;johson&#39;,
  &#39;Lime&#39;,    &#39;Key&#39;,
  &#39;Freeman&#39;, &#39;Chell&#39;,
  &#39;Shepart&#39;, &#39;Furious&#39;
]
[
  &#39;Dave&#39;,    &#39;johson&#39;,
  &#39;Lime&#39;,    &#39;Key&#39;,
  &#39;Freeman&#39;, &#39;Chell&#39;,
  &#39;Shepart&#39;, &#39;Furious&#39;
]

where the elements are not swapped. I do not understand either.

huangapple
  • 本文由 发表于 2023年2月19日 06:01:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定