开发者

why this javascript expression returns "e"?

开发者 https://www.devze.com 2023-02-21 03:19 出处:网络
the following expression returns \"e\" alert([\"a\",\"b\",\"c\",\"d\",\"e\"][[1,2],3开发者_运维知识库,4]);

the following expression returns "e"

alert(["a","b","c","d","e"][[1,2],3开发者_运维知识库,4]);

can anybody tell me why? thanks!


All you need to do is break down the expression:

[
    [1,2],
    3,
    4 
]

You are using bracket notation to access a property on the array literal. The syntax requires an expression. The syntax of an expression allows a single expression to contain many expressions when separated by a comma. Each term of the expression is evaluated left to right and the final value is actually the value of the last term. So your example can be replaced with this:

alert(["a","b","c","d","e"][4]);


["a","b","c","d","e"] is an array and "e" has index 4 in this array. The second part of this expression accesses the array element, for example, if you try ["a","b","c","d","e"][4] you will get "e". I think this part of expression "[1,2],3," is ignored.

0

精彩评论

暂无评论...
验证码 换一张
取 消