码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript 语句分号的必要性

时间:2015-06-28 15:42:27      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:javascript 语句分号的必要性

     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 语句分号的必要性

标签:javascript 语句分号的必要性

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!