Javascript Conflicting Syntax Between Function As Object Key And Labeled Function In Block
Solution 1:
You can reduce the question to: How does the browser know whether {
starts a block and when does it start an object literal?
And the answer to that is that JS engines will treat {
as the start of a block if it appears in a statement position and as the start of an object literal if it is in an expression position.
That's the reason why you have to add parenthesis (()
) around {}
when they appear in a statement position but you want an object instead.
The introduction of labeled function declarations doesn't change the circumstances at all because the situation was already ambiguous:
{
foo: 42
}
Looking at the spec again, this ambiguity is actually pointed out:
An ExpressionStatement cannot start with a U+007B (LEFT CURLY BRACKET) because that might make it ambiguous with a Block.
(and the grammar reflects that too)
Solution 2:
Well... I think that:
if(1){ // the brackets here belong to the if statement == block
L: function F(){}
}
While here:
console.log({ // the brackets represent JSON (javascript object notation) L: functionF(){}
})
This is indeed an object with 'L' index
Post a Comment for "Javascript Conflicting Syntax Between Function As Object Key And Labeled Function In Block"