为什么匿名函数本身就是 javascript 中的语法错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12329731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:56:48  来源:igfitidea点击:

Why is an anonymous function on its own a syntax error in javascript?

javascriptparsingsyntaxgrammaranonymous-function

提问by Flash

If I try to execute a script whose only source line is an object:

如果我尝试执行一个脚本,其唯一的源代码行是一个对象:

{prop:'value'}

it parses fine (in both V8 and UglifyJS). Similarly I can put a string or number on its own as source code and there is no syntax error reported.

它解析得很好(在 V8 和 UglifyJS 中)。同样,我可以将字符串或数字作为源代码单独放置,并且没有报告语法错误。

However, both V8 and UglifyJS complain about this on its own:

然而,V8 和 UglifyJS 都自己抱怨这个:

function(){}

I get Uncaught SyntaxError: Unexpected token (.

我明白了Uncaught SyntaxError: Unexpected token (

Why does this break when the object in the first example is fine? Aren't functions just objects in javascript?

当第一个示例中的对象很好时,为什么会中断?函数不就是 javascript 中的对象吗?

I realise declaring an anonymous function without executing it won't do anything; that's not the question. I want to know why it causes a parse error.

我意识到声明一个匿名函数而不执行它不会做任何事情;这不是问题。我想知道为什么会导致解析错误。

回答by BoltClock

From the ECMAScript spec, section 12.4 on expression statements:

来自 ECMAScript 规范,关于表达式语句的第 12.4 节:

Note that an ExpressionStatementcannot start with an opening curly brace because that might make it ambiguous with a Block. Also, an ExpressionStatement cannot start with the functionkeyword because that might make it ambiguous with a FunctionDeclaration.

请注意,ExpressionStatement不能以左花括号开头,因为这可能会使它与Block不明确。此外, ExpressionStatement 不能以function关键字开头,因为这可能会使它与FunctionDeclaration不明确。

Although functions are just objects, remember that you can declare functions on their own without really making use of their objects in expressions. That's where the ambiguity lies. Granted, you can never declare an anonymous function on its own (as you won't be able to reference it anyway), but as I can't find anything in the spec that distinguishes between anonymous function and named function declarations, I suspect this applies to both.

尽管函数只是对象,但请记住,您可以自行声明函数,而无需在表达式中真正使用它们的对象。这就是歧义所在。当然,您永远不能单独声明匿名函数(因为无论如何您都无法引用它),但是由于我在规范中找不到任何区分匿名函数和命名函数声明的内容,我怀疑这一点适用于两者。

In order to resolve the ambiguity you need to wrap it in parentheses, so it will always be treated as an expression:

为了解决歧义,您需要将其括在括号中,因此它将始终被视为表达式:

(function(){})

回答by xdazz

{prop:'value'}is not parsed as an object, just parsed as a block, which has a label prop.

{prop:'value'}没有被解析为一个对象,只是被解析为一个块,它有一个标签prop

You need ()to enclose it to parsed as an expression.

您需要()将其括起来以解析为表达式。

({prop: 'value'})will be parsed as an object expression.

({prop: 'value'})将被解析为对象表达式。

(function(){})will be parsed as a function expression.

(function(){})将被解析为函数表达式。