JavaScript不带分号的代码风格和缩小? [英] JavaScript semicolon-less code style and minification?

查看:90
本文介绍了JavaScript不带分号的代码风格和缩小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试决定是否值得使用JavaScript无分号样式。

I am trying to decide if it worth to go with JavaScript semicolon-less style.

如果我有没有分号的JavaScript代码:

If I have JavaScript code without semicolon:

function(){
    var first = 1
    var second = 2
    sum = 1 + 2
    return sum
}

它将在浏览器中工作并且 Node.js

it will work in browser and Node.js.

但是会缩小(通过Uglify或< a href =https://en.wikipedia.org/wiki/Google_Closure_Tools#Closure_Compiler =noreferrer> Closure Compiler )代码在浏览器和Node.js中工作?

But will minified (by Uglify or Closure Compiler) code work in browser and Node.js?

我读过 JavaScript中的分号是可选的 的。它说它应该可以工作。

I've read the article Semicolons in JavaScript are optional. it says that it should work.

推荐答案

我个人是一个支持分号的人,但有一个令人信服的论点替代方案,通常不是给予公平的声音。与所有编码风格的论点一样,这将是一场永无止境的辩论,没有合理的结论。做你和你的团队感觉更舒服的事情。

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.

如果你想采用节俭的分号方法,请确保你和你的团队正确理解自动分号插入(ASI)和语句终止规则。无论你的决定是什么,这都是一件好事。

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.

关于缩小:如果你不想冒险使用minifier bug和相关联的麻烦报告和修复(或等待修复)的负担因为它没有正确地进行ASI,所以不要依赖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.

  • Isaacs (Node.js maintainer, and therefore minification agnostic for the purposes of this question) on the subject
  • He recommends this excellent impartial analysis of the ASI rules

这篇关于JavaScript不带分号的代码风格和缩小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆