我必须遵循哪些规则才能编写没有分号的有效 Javascript? [英] What rules must I follow to write valid Javascript without semicolons?

查看:30
本文介绍了我必须遵循哪些规则才能编写没有分号的有效 Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于我应该使用分号吗?"的问题;和分号注入是如何工作的?",但我想为已经决定要尽可能避免使用分号的编码人员找到无 FUD 的建议.

There are lots of questions about "should I use semicolons?" and "how does semicolon injection work?", but I wanted to find FUD-free advice for a coder who has decided he want to avoid using semicolons as much as possible.

如果有人,例如 izs 或 Bootstrap 开发人员,选择在什么条件下编写不带分号的 Javascript 他们必须添加一个分号,他们还应该做些什么来确保他们的代码不会失败?

If someone has, like izs or the Bootstrap devs, chosen to write Javascript without semicolons, under what conditions must they add a semicolon, and what else should they do to ensure their code will not fail?

推荐答案

避免意外合并两行

假设您打算使用换行符代替分号,一个经常被忽视的错误是将两行合并为一个表达式的代码.

Avoid accidentally merging two lines

Assuming you intend to use a newline in place of semicolons, a commonly overlooked mistake is code that combines two lines into a single expression.

例如以下代码:

  // DANGEROUS
  a = b + c
  (d + e).print()

将被解释为:

  a = b + c(d + e).print()

解决办法是:

  // SAFE
  a = b + c
  ;(d + e).print()

类似的问题出现在:

  // DANGEROUS
  console.log()
  ['three', 'two', 'one'].forEach(console.log)

所以一般来说,建议遵循的规则是:

So in general, the recommended rule to follow is:

当一行以括号([

或带有算术运算符 + - * / 或正则表达式 /.../

or with an arithmetic operator + - * / or regexp /.../

然后在它前面放一个分号;.

(但请注意,++ --// 在没有分号的情况下是安全的.)

(Note however that ++ -- and // are safe without a semicolon.)

(您实际上永远不会单独编写 */,因此以这种方式开始一行是没有意义的,但有时您可能想要开始一个带有 /.../ 正则表达式的行.在这种情况下,如果不使用分号,这很可能会导致语法解析错误.( [ + - 情况更危险,因为它们会导致运行时错误.)

(You would never actually write * or / alone, so it wouldn't make sense to start a line that way, but you might sometimes want to start a line with a /.../ regexp. In that case, if a semicolon is not used, this will most likely cause a syntax parsing error. The ( [ + - cases are more dangerous, because they would cause a runtime bug.)

来源:

出于类似的原因,分号也是可取的在库脚本的开头和结尾,它将被释放到野外.事实上,无论您在脚本中使用什么风格,都建议这样做.

For similar reasons, semicolons are also advisable at the start and end of a library script which will be released into the wild. In fact this is recommended regardless of what style you use in your script.

在这个带有 IIFE 的示例中:

In this example with an IIFE:

  // my-library.js
  ;(function(){
      ...
  })();

  • 前导分号将确保您的外括号 (...) 不会尝试在脚本尾部调用未终止的表达式,该表达式已在您的之前连接.

    • The leading semicolon will ensure that your outer parentheses (...) will not try to invoke an un-terminated expression at the tail end of the script which has been concatenated before yours.

      结尾的分号是一种很好的做法,以防脚本以 IIFE 开头并忽略以自己的分号开头.

      The trailing semicolon is good practice in case the script concatenated after yours starts with an IIFE and neglected to lead with its own semicolon.

      作为替代方案,Bootstrap 以 + 开头,不将 IIFE 包裹在 (...) 中,并以 ; 结尾>.其他变体以 ! 而不是 +; 开头.

      As an alternative, Bootstrap leads with a +, does not wrap the IIFE in (...), and ends with a ;. Other variations lead with ! instead of + or ;.

      唯一需要分号的其他时间是在传统的 for 循环中:

      The only other time semicolons are required are in traditional for loops:

        for (var i = 0; i < 10; i++) { // good luck doing that without semicolons!
      

      当您以其他方式在一行中放置多个语句时:

        x = obj[k]; delete obj[k]; return asChar(x)
      

      使用 linter

      一些 linter 会检测您的代码何时可能产生意外后果,并鼓励您遵循上述做法.

      Use a linter

      Some linters will detect when your code may have unintended consequences, and encourage you to follow the practices above.

      例如,所谓的 standardjs linter 将检测上述问题.(他们还发布了 eslint config 如果您更喜欢使用 eslint,但是我不确定这些规则有多全面.)

      For example, the so-called standardjs linter will detect the concerns raised above. (They also have released an eslint config if you prefer to use eslint, but I'm not sure how comprehensive those rules are.)

      这篇关于我必须遵循哪些规则才能编写没有分号的有效 Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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