[英]Semicolon after default export
I read here that I don't need to put a semicolon after default exports. 我在这里读到默认导出后我不需要加分号。 So this program has an unnecessary semicolon: 所以这个程序有一个不必要的分号:
export default function() {};
But if my module continues like this: 但如果我的模块继续这样:
export default function() {};
(() => {
// creating a new function scope
})();
then I can't leave the semicolon. 然后我不能留下分号。
So what is going on here? 那么这里发生了什么? The grammar says I don't need the semicolon but if I leave it the code means something else? 语法说我不需要分号,但如果我离开它,代码意味着别的什么?
UPDATE: 更新:
If I leave the semicolon: 如果我留下分号:
export default function() {}
(() => {
// creating a new function scope
})();
then the exported function gets called instead of being exported. 然后调用导出的函数而不是导出函数。 babeljs.io compiles the latter into: babeljs.io将后者编译成:
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = (function () {})(function () {
// creating a new function scope
})();
;
module.exports = exports["default"];
More precisely after it gets called an error is thrown, because the return value of the first function also gets called (but that is not a function). 更准确地说,在调用它之后会抛出一个错误,因为第一个函数的返回值也会被调用(但这不是函数)。 The error I get in chrome is this: 我在chrome中遇到的错误是这样的:
Uncaught TypeError: (intermediate value)(...) is not a function(…)
You don't need to add a semicolon after a export default
when it's followed by a function declaration, that's what the grammar says. 在export default
之后,当它后跟一个函数声明时,你不需要添加分号,这就是语法所说的。
Babel is wrong, I've filed a bug against it. 巴贝尔是错的,我已经提出了反对它的错误 。 That code should be interpreted as exporting the function and then running the IIFE as an IIFE. 该代码应解释为导出函数,然后将IIFE作为IIFE运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.