命名导出函数声明

时间:2016-12-01 22:14:46

标签: javascript ecmascript-6 es6-modules

首先,这不是关于ASI的问题。我不是在问这里是否适用自动分号插入(好吧,我有点喜欢,但是这个开场陈述是为了避免我是否应该使用分号之间的争论,因为asi会采取分号为我照顾它...)

我知道在函数声明后不要加分号...

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

export函数声明后我需要分号吗?

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

在任何一种情况下,我都很想知道原因。

2 个答案:

答案 0 :(得分:5)

不,你不需要分号,但添加一个也不会有任何伤害。

如果我们查看the ES6 spec,您会看到此签名被视为声明,并且与正常的函数声明一样,之后不需要分号:

  

export *

在该文档中注明了分号(无论是明确的还是隐含的)需要遵循的语句。例如:

  

; FromClause ;

$("<div/>", { "id": "myId", "class": "myClass", "html": "Hello world!" }).appendTo("#myContainer"); 是强制性的。在声明中,它不是。当然,插入分号不会造成任何伤害; JS解释器会将其视为空语句。

答案 1 :(得分:2)

不,你在这里不需要分号。请参阅MDN中的此示例:

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

另见the ECMAScript specification

  

语法

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] ;

如您所见,Declaration之后没有分号。

相关问题