简体   繁体   English

命名导出函数声明后是否需要分号?

[英]Do I need a semicolon after a named export function declaration

First, this is NOT a question about ASI. 首先,这不是关于ASI的问题。 I'm not asking whether or not automatic semicolon insertion applies here (well, I kind of am, but that opening statement is an attempt at avoiding arguments between whether I should or shouldn't use a semicolon because asi will take care of it for me...) 我不是在问这里是否适用自动分号插入(好吧,我有点喜欢,但是这个开场陈述是为了避免我是否应该使用分号之间的争论,因为asi会照顾它我...)

I know not to put a semicolon after a function declaration... 我知道在函数声明后不要加分号...

function foo() { 
  // do stuff
} // no semicolon

But do I need a semicolon after export ing a function declaration? 但是在export函数声明后我需要一个分号吗?

export function foo() {
  // do stuff
} // semicolon or not to semicolon?

In either case, I would also love to know why. 在任何一种情况下,我也想知道为什么。

No, you do not need a semi-colon, though adding one will do no harm. 不,你不需要分号,虽然添加一个也不会有任何伤害。

If we look at the ES6 spec , you will see that this signature is considered a declaration and, like normal function declarations, does not need a semicolon after it: 如果我们查看ES6规范 ,您将看到此签名被视为声明,并且与正常的函数声明一样,在它之后不需要分号:

export Declaration

The statements that need to be followed by a semi-colon (whether explicit or implicit) are noted as such in that document. 在该文档中注明了需要由分号(无论是明确的还是隐式的)所遵循的语句。 For example: 例如:

export * FromClause ; export * FromClause ;

There the ; 那里; is mandatory. 是强制性的。 In the declaration, it is not. 在声明中,它不是。 Of course, inserting the semicolon will not do any harm; 当然,插入分号不会造成任何伤害; the JS interpreter will treat it as an empty statement. JS解释器会将其视为空语句。

No, you don't need a semicolon here. 不,你这里不需要分号。 See this example from MDN : MDN查看此示例:

 export default function() {} // or 'export default class {}' // there is no semi-colon here 

See also the ECMAScript specification : 另请参阅ECMAScript规范

Syntax 句法

 ExportDeclaration : export * FromClause ; export ExportClause[~Local] FromClause ; export ExportClause[+Local] ; export VariableStatement[~Yield, ~Await] export Declaration[~Yield, ~Await] export defaultHoistableDeclaration[~Yield, ~Await, +Default] export defaultClassDeclaration[~Yield, ~Await, +Default] export default[lookahead ∉ { function, async [no LineTerminator here] function, class }]AssignmentExpression[+In, ~Yield, ~Await] ; 

As you see, there's no semicolon after Declaration . 如你所见, Declaration后没有分号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM