为什么JavaScript中的[5,6,8,7] [1,2] = 8? [英] Why does [5,6,8,7][1,2] = 8 in JavaScript?

查看:122
本文介绍了为什么JavaScript中的[5,6,8,7] [1,2] = 8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法绕过这个怪癖。

[1,2,3,4,5,6][1,2,3]; // 4
[1,2,3,4,5,6][1,2]; // 3

我知道 [1,2,3] + [1 ,2] =1,2,31,2,但我找不到正在执行的类型或操作。

I know [1,2,3] + [1,2] = "1,2,31,2", but I can't find what type or operation is being performed.

推荐答案

[1,2,3,4,5,6][1,2,3];
      ^         ^
      |         |
    array       + — array subscript access operation,
                    where index is `1,2,3`,
                    which is an expression that evaluates to `3`.

第二个 [...] 不能是一个数组,所以它是一个数组下标操作。并且下标操作的内容不是操作数的分隔列表,而是单个表达式。

The second [...] cannot be an array, so it’s an array subscript operation. And the contents of a subscript operation are not a delimited list of operands, but a single expression.

了解有关逗号运算符的更多信息在其中

Read more about the comma operator here.

这篇关于为什么JavaScript中的[5,6,8,7] [1,2] = 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆