简体   繁体   English

有人可以解释一下 javaScript 中的“空语句”如何受到“自动分号插入”的影响

[英]Can someone explain me how the “empty statement” in javaScript is affected from “Automatic Semicolon Insertion”

Can someone explain me how the "empty statement" in javaScript is affected from "Automatic Semicolon Insertion" The MDN website states that the empty statement is affected from automatic semicolon insertion though its not stated in the ECMAScript specification can anyone explain this to me and can anyone explain me what is the difference between a semicolon and an empty statement有人可以解释一下 javaScript 中的“空语句”如何受到“自动分号插入”的影响MDN 网站声明空语句受到自动分号插入的影响,尽管它在ECMAScript 规范中没有说明,任何人都可以向我解释这个并且可以谁能解释一下分号空语句有什么区别

Read the spec linked from the MDN page.阅读从 MDN 页面链接的规范。 The empty statement is "affected" because ASI won't be done if the inserted semicolon would result in an empty statement.空语句“受影响”,因为如果插入的分号会导致空语句,则不会执行 ASI。

Hand-wringing over when ASI happens can be dispensed with by simply including semicolons explicitly.只需明确地包含分号,就可以避免在 ASI 发生时费尽心思。 The most common pitfall of ASI is the return statement, when attempting something like ASI 最常见的陷阱是return语句,当尝试类似

return
  { propertyName: "something" };

Don't do that.不要那样做。 Start the object initializer on the same line as the return .在与return相同的行上启动 object 初始化程序。

As to the difference between a semicolon and an empty statement: a semicolon is a boundary.至于分号和空语句的区别:分号是一个边界。 In the following code:在以下代码中:

var x; ;

There's a var declaration statement, then an empty statement.有一个var声明语句,然后是一个空语句。 The second semicolon is not part of the empty statement, but it implies that there is an empty statement before it.第二个分号不是空语句的一部分,但它暗示在它之前一个空语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM