自动分号插入的标准是什么? [英] What are the criteria for automatic semicolon insertion?

查看:131
本文介绍了自动分号插入的标准是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript自动分号插入的规则是什么?

JavaScript因其隐式行终止而迷惑我。它是一种非常类似C的语言,除了分号中的结尾行通常是可选的。

JavaScript befuddles me with its implicit line termination. It's a very C-like language, except that ending lines in a semi-colon is often optional.

那么它如何决定何时假设一个行尾?

So how does it decide when to assume an end-of-line?

考虑这个例子:

var x = 1 + 2
-3 + 3 == 0 ? alert('0') : alert('3')

将其打入HTML文件并打开它在Safari中弹出 3 。如果在第一行的末尾粘贴分号,它将更改为 0

Punching that into an HTML file and opening it in Safari popped up 3. If you stick a semicolon on the end of the first line, it changes to 0.

算法和逻辑都很简单;我感兴趣的是JavaScript决定了什么标准,在这种情况下,不要假设第一行之后的行尾。它是否仅在假设EOL之前等待错误情况?或者是否有更明确的标准?

The algorithms and logic are all straightforward; what interests me is by what criteria JavaScript decided, in this instance, not to assume an end-of-line after the first line. Is it that it only waits for an error scenario before assuming an EOL? Or is there a more definite criteria?

我很好奇。我没有研究过这么多;我想看看S / O社区对此有什么看法。无论如何,我总是以分号结束我的行,但是我有一些JS压缩代码,当我不经意地留下分号时会不时出现分号问题。

I'm very curious. I haven't researched this much; I want to see what the S/O community has to say about it. I always end my lines with semicolons anyway, but I have some JS compression code that trips on the semicolon issue from time to time when I inadvertently leave one out.

编辑

好的只是为了澄清实际问题在这里:任何人都可以用非抽象术语描述JavaScript会自动插入分号。

OK just to clarify what the actual question is here: Can anybody describe, in non-abstract terms, when JavaScript will and won't automatically insert semicolons.

这不是重复的。我知道自动分号插入的规则已经很好地建立并且简明扼要地记录下来。它们也是长篇大论而且令人困惑,因为它们通常是抽象的。根据我的经验,高级程序员不会消化低级文档以及简单的最终结果,这正是我正在寻找的。

This is NOT a duplicate. I'm aware that the rules for automatic semicolon insertion are well established and concisely documented. They're also long winded and confusing because they are generally abstract. In my experience, high level programmers don't digest low level documentation as well as simple end results, which is what I'm looking for.

推荐答案

ECMA规范 (ch.7.9.1,第26页)陈述:

The ECMA specification (ch. 7.9.1, page 26) states:



分号插入有三个基本规则:

There are three basic rules of semicolon insertion:


  1. 当程序从左到右解析时,会遇到一个令牌(称为
    违规令牌),
    如果
    以下条件中的一个或多个为真,则在
    违规令牌之前自动插入分号是
    不允许任何产生
    的语法:


    违规令牌与
    之前的令牌分开至少一个
    LineTerminator。

    •违规令牌
    是}。

  2. 当从左到右解析程序时,遇到输入
    标记流的结尾,并且
    解析器无法解析
    输入令牌流作为单个
    完整的ECMAScript程序,然后
    分号自动插入输入流末尾的

  3. 当程序从左到右解析时,会遇到一个令牌
    ,这是某些
    生成语法所允许的,但是生产是一个
    限制生产,令牌
    将成为
    终端的第一个令牌或非终结点
    ,在$ b内注释[no
    LineTerminator here]之后$ b限制生产(因此
    这样的令牌被称为受限制的
    令牌),受限令牌是
    与前一个令牌分开
    至少一个LineTerminator,然后在限制令牌之前,
    分号会自动插入


I认为这个实现与第二点有关:

I think this implementation has to do with the second point where:

var x = 1 + 2
-3 + 3 == 0 ? alert('0') : alert('3')

可以解析为 a单个完整的ECMAScript程序

因为并不总是清楚解析器将如何插入分号,所以建议不要将它留给解析器(即总是插入你自己的冒号)。

Because it's not always clear how the parser will insert semi-colons, it's advisable to not leave it to the parser (i.e. always insert the semi-colons yourself).

在ch。 7.9.2(自动分号插入的例子)相同的规格这个例子看起来像你的情况:

In ch. 7.9.2 (Examples of Automatic Semicolon Insertion) of the same specs this example looks like your situation:


来源

The source

a = b + c  
(d + e).print()

不会被自动
分号插入转换,因为
带括号的表达式以
开头第二行可以解释为
函数调用的参数列表:

a = b + c(d + e).print()

这篇关于自动分号插入的标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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