Skip to content Skip to sidebar Skip to footer

When Does Js Interpret {} As An Empty Block Instead Of An Empty Object?

I was reading the answer to this question (about the 'wat' video) and it said: {}+[] This is interpreted as an empty block of code, unary plus and empty array. First part does not

Solution 1:

Let's look at the language grammar, shall we? Section 12, Statements:

Statement :
    Block
    VariableStatement
    EmptyStatement
    ExpressionStatement
    ...lots of other stuff...

That's a very fancy way of saying that a statement can be a block, a variable statement, an empty statement, an expression statement, or lots of other stuff. Notice that the first option there is a 'Block':

Block :
    { StatementList(opt) }

StatementList :
    Statement
    StatementList Statement

Which is again, a fancy way of saying that a block is a {, optionally followed by a bunch of statements, followed by a }.

And that's what you see in your example: Before the JavaScript parser thinks that what you have could be an object literal (which is defined somewhere under ExpressionStatement, the 4th thing a 'Statement' could be), it first thinks that you have a 'Block'.

Edit: If you want, you can see it live in a JavaScript engine's source code:

Regarding your second question, that's been covered to great detail on this question. To summarise in a sentence: Node.js treats your input as if it were an expression (thus it can't be a 'Block'), while Firebug/Chrome dev tools treat it like a 'Statement'.

Solution 2:

When the first token in a new statement is {, then {} is interpreted as an empty block.

(Actually of course when { appears after the header clause of something like if or while, then {} is an empty block too, but that's not the interesting case.)

Thus in any other context, like say an argument to a function:

foo({});

the {} is interpreted as an empty object literal.

This situation is similar to the way in which the function keyword is treated differently when it's the first thing in a statement. The syntax has ambiguity, and the parser solves the problem with fixed rules.

Post a Comment for "When Does Js Interpret {} As An Empty Block Instead Of An Empty Object?"