开发者

why this javascript expression returns "e"?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜