JavaScript has a lot of holes in its grammar, which is known to all.
Let's take a picture first.
The code is as follows:
Copy Code code as follows:
{} + []; 0
[] + {}; "[Object Object]"
{} + [] == [] + {}; False
({} + [] == [] + {}); True
The grammatical pit of such an egg ache is estimated to be only a wonderful thing like JavaScript.
I believe that most of the children do not study the JavaScript compiler of the child's shoes, simply can not understand. (At least I think it's incredible)
Later a special go to the Niang a bit, just a little enlightened!
Now let's look at this code:
Copy Code code as follows:
Believe that most children's shoes, the first eye will think that this is a direct amount of objects.
What about this code?
Copy Code code as follows:
Does the browser prompt for syntax errors?
Obviously not! Think it over, and we'll see that this is a statement block.
Copy Code code as follows:
if (isnumber) {
var a = 1;
}
Speaking of which, the keen you may have found: JavaScript in the beginning of {, there will be ambiguity.
What does the JavaScript compiler do with this ambiguity?
To solve this problem, the ECMA method is very simple and rough: when parsing, if a statement begins with a "{", it is interpreted as a block of statements.
This is really a pit dad's way of handling!
Since it is a statement block, then why {a:1} but no grammatical errors?
In fact, here, a is understood by the parser as a label. tags are used to match break and continue statements for directional jumps.
As a result, such a notation throws an exception:
Copy Code code as follows:
The function () {} is not a function declaration, nor is it an expression.
Here, we should have a basic concept of the wonderful processing of {}. Let's look at some of the statements that were mentioned at the beginning of the article:
Copy Code code as follows:
{} + []; 0
[] + {}; "[Object Object]"
{} + [] == [] + {}; False
({} + [] == [] + {}); True
First, because {} is a statement block, the code can be understood to be:
Copy Code code as follows:
So the return value is 0.
The second, because {} is not at the beginning of the statement, is a normal object direct quantity, empty array and empty object add directly, return "[Object]".
To understand the first and second article, the third article has no need to explain.
Fourth, since it is () started, the first {} is resolved to the object's direct amount, so that the two formulas are equal, and return true.