英文:
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 = ["Dave", "johson", "Lime", "Key", "Freeman", "Chell", "Shepart", "Furious"];
[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 = [
"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'
]
[
'Dave', 'johson',
'Lime', 'Key',
'Freeman', 'Chell',
'Shepart', 'Furious'
]
[
'johson', 'Dave',
'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论