Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当添加第二个索引时,为什么在javascript数组中覆盖第一个索引?_Javascript - Fatal编程技术网

当添加第二个索引时,为什么在javascript数组中覆盖第一个索引?

当添加第二个索引时,为什么在javascript数组中覆盖第一个索引?,javascript,Javascript,例如,当我输入以下代码时,答案是10 var testArry = [20, 10, 90, 50, 66][1]; console.log(testArry); // output: 10 var testArry = [20, 10, 90, 50, 66][1,2]; console.log(testArry); // output: 90 当我添加另一个索引时,第一个索引被覆盖,答案是90 var testArry = [20, 10, 90, 50, 66][1]; console

例如,当我输入以下代码时,答案是10

var testArry = [20, 10, 90, 50, 66][1];
console.log(testArry); // output: 10
var testArry = [20, 10, 90, 50, 66][1,2];
console.log(testArry); // output: 90
当我添加另一个索引时,第一个索引被覆盖,答案是90

var testArry = [20, 10, 90, 50, 66][1];
console.log(testArry); // output: 10
var testArry = [20, 10, 90, 50, 66][1,2];
console.log(testArry); // output: 90
什么是超越的原因?这是最重要的还是其他术语

我在谷歌上搜索了一下,但没有找到想要的原因


谢谢。

在数组索引表达式(1,2)中,您使用的是逗号运算符,它只返回最后一个结果,在本例中为2,而对其他结果(1)不做任何处理。因此,您基本上只需编写
testArray[2]

1,2
计算结果为2<代码>1,3,4,5,10评估为10。执行的最后一个表达式是返回值。附加说明:基本上,它只是使用最后一个值。如果您有[1,2,3],则使用3会导致50。如果指定8作为最后一个值,那么结果将是未定义的,因为索引将超出范围。