ES6中默认导出。为什么不需要分号? [英] Default export in ES6. Why don't you need a semicolon?

查看:521
本文介绍了ES6中默认导出。为什么不需要分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于ES6的探索

I'm reading this from the exploringjs about ES6

可以有一个默认导出。例如,一个函数:

17.1.2 Single default export

There can be a single default export. For example, a function:

//------ myFunc.js ------   
export default function () { ··· } // no semicolon!

//------ main1.js ------  
import myFunc from 'myFunc';
myFunc();

或一个类:

//------ MyClass.js ------  
export default class { ··· } // no semicolon!

//------ main2.js ------  
import MyClass from 'MyClass';
let inst = new MyClass();

请注意,如果
默认输出一个函数,则最后没有分号一个类(它们是匿名的
声明)。

Note that there is no semicolon at the end if you default-export a function or a class (which are anonymous declarations).

为什么不在结尾处使用分号出口默认声明?我以为你以分号结尾的所有陈述?

Why do you not use a semi-colon at the end of the export default declaration? I thought you end all statements with semi-colons?

推荐答案


为什么不需要分号?

Why don't you need a semicolon?

因为语法没有在这里定义分号:

Because the grammar doesn't define a semicolon there:

export default HoistableDeclaration
export default ClassDeclaration
export default [lookahead ∉ {function, class}] AssignmentExpression ;

(除非您有表达式)


我以为你以分号结尾所有的陈述?

I thought you end all statements with semi-colons?

根本不是这样。你有没有把分号放在一个块之后?有人写了

That's not true at all. Have you ever put a semicolon after a block? Is someone writes

if (...) {

};

然后是错误的。

似乎所有的语句都以分号终止,因为在大多数语句的末尾,你最终会有一个 ExpressionStatement 或空的语句,它们都以分号结尾。

It may seem that all statements are terminated by semicolons, because at the end / bottom of most statements, you end up having an ExpressionStatement or empty statement, which both are terminated by semicolons.

除此之外, ExportDeclaration 不是声明。

这篇关于ES6中默认导出。为什么不需要分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆