简体   繁体   English

括号是否可以防止分号插入?

[英]Do parentheses prevent semicolon insertion?

Say that I want to set a bunch of variables equal to null (and don't want to use an array/loop structure) or maybe I just want to write a large boolean expression across multiple lines. 假设我想设置一组等于null的变量(并且不想使用数组/循环结构)或者我只想在多行中编写一个大的布尔表达式。 Does an unclosed parenthetical prevent semicolon insertion in such a case? 在这种情况下,未公开的括号是否会阻止分号插入? Eg 例如

some_variable = another_variable = yet_another_variable = (
    oh_look_a_parenthesis_above_me = hey_heres_another_variable) = (
    and_for_some_reason_another = last_one) = null;

Or 要么

if(test_for_some_complex_expr && another_test || (
    but_maybe_this_one && or_this_one)) {
    // ...
}

And how does this compare versus using && or || 这与使用&&||相比如何进行比较 or = to group expressions over line breaks? =在换行符上分组表达式? Ie, would this always work too? 即,这总是有效吗?

some_variable = another_variable = 
    a_variable_after_a_line_break = and_one_more;

while(test_for_an_expr && another_test || 
    (an_expr_here && and_an_expr_here)) {
    // ...
}

I'm looking for the way that is most standard across all browsers (including IE6+). 我正在寻找所有浏览器(包括IE6 +)最标准的方式。

Semicolon insertion only happens if you have code which would be a syntax error without the semicolon. 如果你的代码是没有分号的语法错误,则只会发生分号插入。 In your case the expression is perfectly valid, so you don't need to worry about semicolon insertion - parentheses or not. 在您的情况下,表达式是完全有效的,因此您不必担心分号插入 - 括号或不括号。

An example where semicolon insertion will happen: 以分号插入的示例:

var a = 1
var b = 2

In the above case, a semicolon is inserted at the line break, but only because it is a syntax error to write: 在上面的例子中,在换行符处插入分号,但这只是因为写入语法错误:

var a = 1 var b = 2

but it is perfectly valid to write: 但写完是完全有效的:

var a = 1; var b = 2

The rules get tricky because there are some instances in JavaScript syntax where line breaks are NOT allowed. 规则变得棘手,因为JavaScript语法中存在一些不允许换行的实例。 For example a line break is NOT allowed between the ' return ' keyword and a value to return. 例如,' return '关键字和要返回的值之间不允许换行。 So this is a syntax error: 所以这是一个语法错误:

return
  17;

But the way semicolon insertion "fixes" this error is by inserting a semicolon like this: 但分号插入“修复”此错误的方式是插入一个像这样的分号:

return;
  17;

Which is probably not what the writer intended! 这可能不是作家的意图! In that particular case, a parenthesis can be used to prevent semicolon insertion: 在该特定情况下,可以使用括号来防止分号插入:

return (
   17);

Because it only between the return keyword and the start of the expression that line break is disallowed. 因为它只在return关键字和表达式的开头之间不允许换行。 Inside an expression it is not a problem. 在表达式中,这不是问题。

Does an unclosed parenthetical prevent semicolon insertion in such a case? 在这种情况下,未公开的括号是否会阻止分号插入?

Yes. 是。 Although it is unnecessary: 虽然没必要:

And how does this compare versus using && or || 这与使用&&||相比如何进行比较 or = to group expressions over line breaks? =在换行符上分组表达式?

It's exactly the same. 它完全一样。 They are expressions that require a second part, therefore ASI cannot - must not - kick in without making the result invalid. 它们是需要第二部分的表达式,因此ASI不能 - 不得 - 在不使结果无效的情况下启动。

ASI will only occur when the continued line is invalid without the semicolon. 只有在没有分号的情况下连续行无效才会发生ASI。 See What are the rules for JavaScript's automatic semicolon insertion (ASI)? 请参阅JavaScript的自动分号插入(ASI)的规则是什么? for details. 详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM