JavaScript 语句分号的必要性
2015-06-28 11:17
716 查看
Statements in ECMAScript are terminated by a semicolon, though omitting the semicolon makes
the parser determine where the end of a statement occurs, as in the following examples:
Even though a semicolon is not required at the end of statements, it is recommended to always
include one. Including semicolons helps prevent errors of omission, such as not finishing what you
were typing, and allows developers to compress ECMAScript code by removing extra white space
(such compression causes syntax errors when lines do not end in a semicolon). Including semicolons
also improves performance in certain situations, because parsers try to correct syntax errors by
inserting semicolons where they appear to belong.
比如下面代码:
输出:
所以,我们还是不要让解析器猜测我们的用意,毕竟解析器不是人工智能的:
正确写法:
the parser determine where the end of a statement occurs, as in the following examples:
var sum = a + b //valid even without a semicolon - not recommended var diff = a - b; //valid - preferred
Even though a semicolon is not required at the end of statements, it is recommended to always
include one. Including semicolons helps prevent errors of omission, such as not finishing what you
were typing, and allows developers to compress ECMAScript code by removing extra white space
(such compression causes syntax errors when lines do not end in a semicolon). Including semicolons
also improves performance in certain situations, because parsers try to correct syntax errors by
inserting semicolons where they appear to belong.
比如下面代码:
"use strict" function f(a, b) { return a | b ; } console.log(f(2, 1));
输出:
所以,我们还是不要让解析器猜测我们的用意,毕竟解析器不是人工智能的:
正确写法:
相关文章推荐
- JavaScript在网页中的位置
- 学习Javascript闭包(Closure)
- 理解 Javascript 的闭包
- 那些逐步掌握的JavaScript('arguments')
- Jsp:param标签的使用
- Extjs布局
- javascript中遇到的字符串对象处理
- js类型判断的方法
- Js学习文件上传
- 最简单的处理MVC中默认的Json方法返回时间的问题
- 100多个基础常用JS函数和语法集合大全
- 【HM】第3课:JavaScript高级
- JS的碰撞周期移动
- JSON处理
- 在JSP前台和后台传递参数乱码汇总
- 11. Firefox如何调试js脚本:
- Array的队列方法&重排序方法—— JS学习笔记2015-6-27(第68天)
- js 函数的参数 问题 arguments对象 及闭包
- JavaScript语法 (顺序+判断+选择+循环+其他=五种语句)演示
- Web 开发的 JavaScript 框架资料收集(15款)