简体   繁体   English

if语句中有两组括号,这是什么意思?

[英]Two sets of parentheses inside an if statement, what does that mean?

Recently I saw some weird code that looks kinda like this: 最近,我看到了一些看起来像这样的怪异代码:

let a;
if ((a = anotherVariable)) {
 ....
}

Can you explain to me what does this mean and why is it done? 您能告诉我这是什么意思,为什么要这样做?

Edit: 编辑:

Notice that he's not comparing a to anotherVariable , he's assigning it. 请注意,他没有将aanotherVariable进行比较,而是在分配它。 And there are no other terms in the if parentheses, so it's not like if ((something) && (something else)) {...} . 而且if括号中没有其他术语,因此不像if ((something) && (something else)) {...}

So why not just assign a variable separately, and then check for truthiness of anotherVariable ? 那么,为什么不只是单独分配a变量,然后检查anotherVariable真实性呢?

Original code here (search for matched = prevTextFromSpace ). 原始代码在这里 (搜索matched = prevTextFromSpace )。

If you write 如果你写

if (a = anotherVariable) {
 ....
}

it works fine but = is an assignment, not a comparison. 它工作正常,但=是分配,而不是比较。

The value of the expression a = b is the value of b (and it is also stored in the variable a ). 表达式a = b的值是b的值(它也存储在变量a )。 If b evaluates to a truish value then the if branch is taken. 如果b评估为稳定值,则采用if分支。

However, because = is not == , the linter programs usually flag an if statement as above as a potential error. 但是,由于=不是== ,因此linter程序通常将上述if语句标记为潜在错误。 It happens many times that the developer wanted == and typed = by mistake. 开发人员错误地想要==和键入=的情况经常发生。 The linters try to detect this error. 短毛猫试图检测到此错误。

If the real intention is to copy the value of b in a and also use this value as the condition of the if statement (and not to compare a to b ) then the code above is correct. 如果真正意图是的值复制ba ,还可以使用这个值作为条件if语句(而不是比较ab ),那么上面的代码是正确的。 There are two ways to silent the linter warning: 有两种方法可以使棉短绒警告静音:

  1. Put the assignment in front of the if statement: 将赋值放在if语句之前:

     a = b if (a) { ... } 
  2. Put the assignment inside a pair of parentheses: 将作业放在一对括号中:

     if ((a = b)) { ... } 

    This decouples the assignment from the if . 这使分配与if分离。 The assignment is not the if condition any more. 分配不再是if条件。 The assignment is just a sub-expression of the larger expression (a = b) . 赋值只是较大表达式(a = b)的子表达式。

    This larger expression is the condition of the if statement and it is not an assignment, so there is nothing to warn about. 这个较大的表达式是if语句的条件,它不是赋值,因此没有任何警告。 Yes, it contains an assignment but the assignment is just a sub-expression. 是的,它包含一个分配,但分配只是一个子表达式。

It looks like a hack but it is not. 它看起来像骇客,但事实并非如此。 It only denotes a good understanding of expressions and statements in programming languages. 它仅表示对编程语言中的表达式和语句有很好的理解。

The inner parenthesis simply evaluates the expression it contains. 内括号只是评估它包含的表达式。 It will assign the value of anotherVariable to the variable a and that new value will be compared with the other operand in the condition. 它将为变量a分配anotherVariable的值,并将新值与条件中的另一个操作数进行比较。

 let a = 2, anotherVariable=20; if ((a = anotherVariable) == 20) { console.log(a) } 

It doesn't actually do anything except group the components. 除了对组件进行分组之外,它实际上不执行任何操作。 Let's say it looked like this: 假设它看起来像这样:

if (a && (b || c))

Here, the parentheses would matter, because without them, JavaScript would interpret them incorrectly IIRC, as it'd look like this: 在这里,括号很重要,因为没有它们,JavaScript会错误地将它们解释为IIRC,如下所示:

if (a && b || c)

And it's hard to tell what that'd output. 而且很难说会输出什么。 Grouping is also important when you're using multiple ternary operators, like this: 当您使用多个三元运算符时,分组也很重要,如下所示:

if ((a : b ? c) && (d : g ? f))

Here, the parentheses are extremely important - bit of an extreme example, I know, but it illustrates my point nicely. 在这里,括号非常重要-我知道一个极端的例子,但这很好地说明了我的观点。

In short, parentheses grouping is a good habit to get into in JavaScript, as even when you have extremely simple conditions like: 简而言之,括号分组是进入JavaScript的一种好习惯,即使您有以下非常简单的条件,例如:

if ((a > b) && (c < d))

It's very easy to get confused when you are meant to have two or more layers of parentheses. 当您打算使用两层或多层以上的括号时,很容易感到困惑。 Imagine thinking through this with no parentheses! 想象一下,没有括号地思考!

if ((((a ? b : c) == (d < e)) && (f !== g)) || !(h == j))

Hopefully this helps! 希望这会有所帮助!

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

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