【问题标题】:Do I need a semicolon after a named export function declaration命名导出函数声明后是否需要分号
【发布时间】:2016-12-01 22:14:45
【问题描述】:

首先,这不是关于 ASI 的问题。我不是在问这里是否适用自动分号插入(嗯,我有点,但开场白是为了避免在我是否应该使用分号之间发生争论,因为 asi 会处理它我……)

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

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

但是在export函数声明之后我需要一个分号吗?

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

无论哪种情况,我也很想知道原因。

【问题讨论】:

    标签: javascript ecmascript-6 es6-modules


    【解决方案1】:

    不,您不需要分号,尽管添加一个不会有任何害处。

    如果我们查看the ES6 spec,您会发现此签名被视为声明,并且与普通函数声明一样,它后面不需要分号:

    export Declaration

    需要后跟分号的语句(无论是显式的还是隐式的)在该文档中都已注明。例如:

    export * FromClause ;

    ; 是强制性的。在声明中,它不是。当然,插入分号不会有任何害处; JS 解释器会将其视为空语句。

    【讨论】:

      【解决方案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 后面没有分号。

      【讨论】:

      • 是的,但那是default export 我特别想知道named exports
      • @sfletche 为什么命名导出会有所不同?
      • 我不知道会不会。 (这是我问这个问题的原因之一)
      猜你喜欢
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多