admin管理员组文章数量:1313822
First, this is NOT a question about 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...)
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 function foo() {
// do stuff
} // semicolon or not to semicolon?
In either case, I would also love to know why.
First, this is NOT a question about 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...)
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 function foo() {
// do stuff
} // semicolon or not to semicolon?
In either case, I would also love to know why.
Share Improve this question edited Mar 24, 2019 at 20:37 double-beep 5,51919 gold badges40 silver badges49 bronze badges asked Dec 1, 2016 at 22:14 sfletchesfletche 49.8k31 gold badges108 silver badges120 bronze badges2 Answers
Reset to default 9No, 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:
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;
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.
No, you don't need a semicolon here. See this example from MDN:
export default function() {} // or 'export default class {}' // there is no semi-colon here
See also the ECMAScript specification:
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
.
本文标签: javascriptDo I need a semicolon after a named export function declarationStack Overflow
版权声明:本文标题:javascript - Do I need a semicolon after a named export function declaration - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741875352a2402428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论