JavaScript 无分号代码样式和缩小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12073960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript semicolon-less code style and minification?
提问by WHITECOLOR
I am trying to decide if it worth to go with JavaScript semicolon-less style.
我正在尝试决定是否值得使用 JavaScript 无分号样式。
If I have JavaScript code without semicolon:
如果我有没有分号的 JavaScript 代码:
function(){
var first = 1
var second = 2
sum = 1 + 2
return sum
}
it will work in browser and Node.js.
它将在浏览器和Node.js 中工作。
But will minified (by Uglify or Closure Compiler) code work in browser and Node.js?
但是缩小(通过 Uglify 或Closure Compiler)代码可以在浏览器和 Node.js 中工作吗?
I've read the article Semicolons in JavaScript are optional. it says that it should work.
我读过文章Semicolons in JavaScript are optional。它说它应该工作。
回答by Raoul
I'm personally a pro-semicolon sort of guy, but there is a compelling argument for the alternative, which is frequently not given a fair voice. As with all coding style arguments this will be a never-ending debate with no reasonable conclusion. Do what you and your team feel more comfortable with.
我个人是一个支持分号的人,但是对于替代方案有一个令人信服的论据,这通常没有得到公平的声音。与所有编码风格的争论一样,这将是一场永无止境的争论,没有合理的结论。做你和你的团队觉得更舒服的事情。
If you do go for a frugal semicolon approach, make sure you and your team properly understand the automatic semicolon insertion(ASI) and statement termination rules. That is definitely a good thing to understand whatever your decision.
如果您确实采用节俭的分号方法,请确保您和您的团队正确理解自动分号插入(ASI) 和语句终止规则。无论您的决定如何,这绝对是一件好事。
With respect to minification: If you don't want to risk the potential headache of a minifier bug and the associated burden of reporting it and fixing it (or waiting for a fix) because it's not doing ASI properly, then don't rely on ASI.
关于缩小:如果您不想冒险承担缩小程序错误的潜在头痛以及报告和修复它(或等待修复)的相关负担,因为它没有正确执行 ASI,那么不要依赖ASI。
- Isaacs (Node.jsmaintainer, and therefore minification agnostic for the purposes of this question) on the subject
- He recommends this excellent impartial analysis of the ASI rules
- 关于这个主题的Isaacs(Node.js维护者,因此就本问题而言与缩小无关)
- 他建议对 ASI 规则进行出色的公正分析
回答by Beat Richartz
Despite that semicolons in JavaScript are optional, there's strong advice against not using semicolons:
尽管 JavaScript 中的分号是可选的,但强烈建议不要使用分号:
It makes your code vulnerable for bugs
resulting from removing whitespace (used in minification):Try this:var a = 1; var b = 7; var sum = a+b (a + b)
它使您的代码容易受到错误的影响
由于删除空格(用于缩小):试试这个:变量 a = 1; 无功b = 7; var sum = a+b (a + b)
it minifies to which will result in the error var a=1,b=7,sum=a+b(a+b);
,number is not a function
. There are also other casesand this case.
它缩小到这将导致错误var a=1,b=7,sum=a+b(a+b);
,number is not a function
。还有其他情况和这种情况。
Update: This bug wasn't resulting from minification, however, the next one is:
更新:这个错误不是由缩小引起的,但是,下一个是:
It makes your code vulnerable for bugs resulting from minification: Try:
var isTrue = true function doSomething() { return 'yeah' } function doSomethingElse() { return 'yes, dear' } doSomething() !isTrue && doSomethingElse()
minifies to:
var isTrue=true;function doSomething(){return "yeah"}function doSomethingElse(){return "yes, dear"}doSomething()!isTrue&&doSomethingElse();
which results in:
SyntaxError: Unexpected token !
It makes your code less readable and maintainable in terms of convenience: Using the semicolon has been rightfully established as good practice, and a trained JavaScript developer will be puzzled by code trying to evade the convention.
它使您的代码容易受到缩小导致的错误的影响:尝试:
var isTrue = true function doSomething() { return 'yeah' } function doSomethingElse() { return 'yes, dear' } doSomething() !isTrue && doSomethingElse()
缩小到:
var isTrue=true;function doSomething(){return "yeah"}function doSomethingElse(){return "yes, dear"}doSomething()!isTrue&&doSomethingElse();
这导致:
SyntaxError: Unexpected token !
它使您的代码在便利性方面的可读性和可维护性降低:使用分号已被正确地确立为良好的实践,训练有素的 JavaScript 开发人员会对试图逃避约定的代码感到困惑。
Another thing is, you have to ask yourself: What do you really gain omitting the semicolon?
另一件事是,你必须问自己:省略分号你真正得到了什么?
- Clean Code? If you want JavaScript to not look like JavaScript, try Coffee Script. But there are some misguided notions, which believe that pitfalls like the one mentioned above are cleared by ?Easy solution: when a line starts with parenthesis, prepend a semicolon to it.?. How is this clean code, and how does that help anyone reading your code?
- 干净的代码?如果您希望 JavaScript 看起来不像 JavaScript,请尝试Coffee Script。但是有一些被误导的概念,他们认为像上面提到的那样的陷阱可以通过 ?Easy solution: 当一行以括号开始时,在它前面加上一个分号。?。这个干净的代码如何,它如何帮助任何人阅读您的代码?
Bottom Line: With minification, I would definitely try to use conventions of JSLint. I've seen some people finally linting their JavaScript code after hours of trying to fix a bug not happening in unminified, but in minified code. Don't put yourself into this misery; semicolons may be ugly at first sight, but they keep the bugs out.
底线:通过缩小,我肯定会尝试使用JSLint 的约定。我看到有些人在尝试修复一个不是在未缩小的代码中而是在缩小的代码中发生的错误数小时后,终于对他们的 JavaScript 代码进行了 linting。不要让自己陷入这种痛苦;分号乍一看可能很难看,但它们可以将错误排除在外。
回答by Imp
The compilers and minifiers are probably intelligent enough to add the semicolons if needed (and if it helps the minifying process).
编译器和压缩器可能足够聪明,可以在需要时添加分号(如果它有助于缩小过程)。
However! A semicolon tells the interpreter that this is the end of a statement. If it is missing, it has to look at the beginning of the next line to find out if it makes sense for the statement to continue. Then it will decide, if the next line is a new statement (i.e. a semicolon was missing) or if it's in fact the continuation of the statement from the previous line.
然而!分号告诉解释器这是一个语句的结尾。如果它丢失了,它必须查看下一行的开头,以确定继续该语句是否有意义。然后它将决定下一行是新语句(即缺少分号)还是实际上是上一行语句的延续。
Without semicolons, you're slowing things down! Not to mention that this could introduce some obscure errors when the interpreter, executing the algorithm I described, decides wrong!
没有分号,你会减慢速度!更不用说当解释器执行我描述的算法时,这可能会引入一些模糊的错误!
回答by Per Salbark
Don't do it. Semi-colon insertion is a very dangerous thing. The size gain is not worth the added risk.
不要这样做。分号插入是一件非常危险的事情。规模增加不值得增加风险。
Doulas Crockford will not like you if you do it :) http://youtu.be/hQVTIJBZook
如果你这样做,Doulas Crockford 不会喜欢你 :) http://youtu.be/hQVTIJBZook
回答by Peter
IMHO, do not do that. You don't really gain that much and semicolon is AFAIK placed there automatically, and it can be put in a place where it will produce weird bugs.
恕我直言,不要那样做。你并没有真正获得那么多,而且分号是自动放置在那里的,它可以放在会产生奇怪错误的地方。