如果条件分成多行,将长 JavaScript 拆分是否安全?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29154339/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:08:10  来源:igfitidea点击:

Is it safe to split long JavaScript if conditions into multiple lines?

javascript

提问by Aniket Suryavanshi

if ( true && true ||
  false && false ||
  true && true ) {
  console.log( 'Splitting condition into multiple lines worked!' );
}

Does the above snippet of code work in all relevant browsers?

上面的代码片段是否适用于所有相关浏览器?

PS: I'm also concerned about IE8 as it has too big a marketshare to ignore as of today.

PS:我也很担心 IE8,因为它的市场份额太大了,现在已经不容忽视了。

回答by Oriol

This is explained in the spec:

这在规范中解释:

7.3 Line Terminators

Like white space characters, line terminator characters are used to improve source text readability and to separate tokens (indivisible lexical units) from each other. However, unlike white space characters, line terminators have some influence over the behaviour of the syntactic grammar. In general, line terminators may occur between any two tokens, but there are a few places where they are forbidden by the syntactic grammar. Line terminators also affect the process of automatic semicolon insertion (7.9).

7.9 Automatic Semicolon Insertion

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-whilestatement, continuestatement, breakstatement, returnstatement, and throwstatement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

7.3 行终止符

与空白字符一样,行终止符用于提高源文本的可读性和将标记(不可分割的词汇单元)彼此分开。但是,与空白字符不同,行终止符对句法语法的行为有一些影响。一般来说,行终止符可能出现在任何两个标记之间,但也有一些地方被句法文法禁止。行终止符也会影响 自动分号插入 (7.9) 的过程

7.9 自动分号插入

某些 ECMAScript 语句(空语句、变量语句、表达式语句、do-while语句、continue语句、 break语句、return语句和throw语句)必须以分号结尾。此类分号可能始终明确出现在源文本中。但是,为方便起见,在某些情况下,源文本中可能会省略此类分号。这些情况的描述是说在这些情况下分号会自动插入到源代码令牌流中。

So in most cases you can do it. And your case is one of these.

所以在大多数情况下你可以做到。你的情况就是其中之一。

回答by Joseph Cho

Browsers are very forgiving when it comes to whitespace and line breaks in conditionals. Right now these two formats are the industry standard. Personally I like the syntax in the second scenario more.

当涉及到条件语句中的空格和换行符时,浏览器非常宽容。现在这两种格式是行业标准。我个人更喜欢第二种场景中的语法。

Scenario A

情景A

if (
  a === 123 &&
  b === 'abc'
) { ... }

Scenario B

情景B

if (
  a === 123
  && b === 'abc'
) { ... }

AirBnb's preferred syntax: https://github.com/airbnb/javascript/issues/1380

AirBnb 的首选语法:https: //github.com/airbnb/javascript/issues/1380

回答by Memmo

I also recently found myself having a very long condition. Below you will find an example of how I solved it, using Boolean():

我最近也发现自己的病情很长。您将在下面找到我如何使用Boolean()解决它的示例:

const a = new Array('', '  ', '    ');
const empty = Boolean(a[0].trim() == '' || 
                      a[1].trim() == '' || 
                      a[2].trim() == '' ||);
if (!empty){
   console.log("Fine!");
}else{
   console.log("Not fine!");   
}

I hope I can help you.

我希望我能帮助你。