Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第6条:了解分号插入的局限 #6

Closed
xiaoluoboding opened this issue Apr 15, 2016 · 0 comments
Closed

第6条:了解分号插入的局限 #6

xiaoluoboding opened this issue Apr 15, 2016 · 0 comments
Assignees

Comments

@xiaoluoboding
Copy link
Owner

xiaoluoboding commented Apr 15, 2016

笔记

  • 分号插入的第一条规则:
    • 分号只能在一行、一个代码块和一段程序借宿的地方省略分号。
  • 分号插入的第二条规则:
    • 分号插入式一种错位校正机制。
  • 有5个明确有问题的字符需要密切注意:([+-/。每一个都能作为一个表达式运算符或者一条语句的前缀,依赖于具体上下文。
  • JavaScript允许逗号分隔表达式。逗号分隔表达式从左至右以此执行,并返回最后一个表达式的值。

function square(x) {
    var n = +x
    return n * n
}
square(4)
/*
16
*/

function area(r) { r = +r; return Math.PI * r * r }
area(4)
/*
50.26548245743669
*/

function add1(x) { return x + 1 }
add1(4)
/*
5
*/

function area(r) { r = +r return Math.PI * r * r }
area(4)
/*
Exception: SyntaxError: missing ; before statement
@Scratchpad/2:1
*/

提示

  • 仅在}标记之前、一行的结束和程序的结束处推导分号。
  • 仅在紧接着的标记不能被解析的时候推导分号。
  • 在以([+-/字符开头的语句前绝不能省略分号。
  • 当脚本连接的时候,在脚本之间显示地插入分号。
  • returnthrowbreakcontinue++--的参数之前绝不能换行。
  • 分号不能作为for循环的头部或空语句的分隔符而被推导出。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant