Javascript Conflicting Syntax Between Function As Object Key And Labeled Function in Block

最后都变了- 提交于 2019-12-12 16:43:43

问题


Assuming you have a browser that supports both labeled function declarations and block statements, what is the standard way/method for browsers to determine if the following is an object with a property named L that is function F, or a block that contains function F labeled as L:

{
    L: function F(){}
}

E.g.

To expose what I mean, here are two different copies of the above code modified to expose it as an array and as a function:

document.body.textContent = typeof( () => {
    L: function F(){}
} )

In the above code, the browser recognizes the arrow function notation and determines that it is a block statement. However,

document.body.textContent = typeof {
    L: function F(){}
}

The above code makes the browser think that it is an object written out as an object literal with index L being function F


回答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)




回答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: function F(){}
})

This is indeed an object with 'L' index



来源:https://stackoverflow.com/questions/44576954/javascript-conflicting-syntax-between-function-as-object-key-and-labeled-functio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!