How do the following expressions get parsed and interpreted?
The chrome console does this:
> {'foo'}{'bar'}
'bar'
and similarly
> {a: 'foo'}{b: 'bar'}
'bar'
(Obviously whatever is going on here shouldn’t be used in real code.)
How do the following expressions get parsed and interpreted?
The chrome console does this:
> {'foo'}{'bar'}
'bar'
and similarly
> {a: 'foo'}{b: 'bar'}
'bar'
(Obviously whatever is going on here shouldn’t be used in real code.)
2
Answers
The 1st expression could be rewritten as
That is, two separate one-line statements, each containing a constant string literal, within otherwise empty code blocks. The result is the last expression evaluated, which is
'bar'
.The second expression can be rewritten as
Where
a
andb
are labels. Again, the last expression evaluates to'bar'
. While this second line looks somewhat similar to an object literal, this is not the case. Note that> {b: 'bar', a: 'foo'}
results in a syntax error.You can use AST Explorer to view AST details.
Code 1:
AST:
So it means two blocks.
Code 2:
AST:
So it means two blocks with two labels.