首页 > 编程语言 > 详细

JavaScript 语句分号的必要性

时间:2015-06-28 15:42:04      阅读:222      评论:0      收藏:0      [点我收藏+]
     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:
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 语句分号的必要性

原文:http://blog.csdn.net/doctor_who2004/article/details/46670137

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!