(1, 2, 3);
// return 3; I’m curious, why does it return 3? What is the mechanism? What role do parentheses play?
(1, 2, 3, alert )("Amazing!");
// What’s even more fun here is that alert can be called directly; after testing, global functions can be called like this.
The following are questions about square brackets []:
In JS, [] is almost equal to the array, but there are also magic moments.
[1,2,3][1];
// return 2; Of course, this method is also often used. It is often used for the index of the array and the response attribute name of the calling object to be stored in a certain variable.
[1,2,3][1,2];
//return 3 ; This case is much more interesting. It is very similar to the case of parentheses above, and will return the last character in the parentheses. A value
in (1,2,3), brackets and commas are both operators. The comma operator returns the evaluation result of the last expression. You can check the specific rules yourself. The brackets in
("Amazing") represent function calls.
alert is a function object that can of course be called when it is returned as the evaluation result of an expression.
[1,2,3] The square brackets here are to declare array constants, followed by [1] to take array elements. [1,2,3][1] means taking the first element in the array [1,2,3], that is, 2 (the subscript starts from 0)
[1,2,3 ][1,2]=[1,2,3][(1,2)]=[1,2,3][2]=3