简体   繁体   English

具有三个变量的短路评估

[英]short-circuit evaluation with three variables

I wrote the following code, thinking it would fail to work because it would go like this: "if it is not running and the ID matches this one, execute the code" disregarding the cap on the break size: 我编写了以下代码,认为它将无法正常工作,因为它会像这样:“如果未运行且ID与此匹配,请执行代码”,而忽略中断大小的上限:

if(!isRunning && id==="break-increment" && breakvar<=59){
this.setState((state) => ({
  break: this.state.break +1}))}

Much to my surprise, it worked. 令我惊讶的是,它奏效了。 But I don´t really understand why. 但是我真的不明白为什么。 Wouldn´t it fail because the cap size would never be evaluated? 是否会因为无法评估瓶盖尺寸而失败?

I thought you couldn´t have three conditions without short-circuiting (eg, if isRunning is false then it only evaluates id) 我以为没有短路就不可能有三个条件(例如,如果isRunning为false,那么它只会计算id)

No. 没有。

If !isRunning is false, the first && short circuits and is evaluated as its LHS (false) without evaluating the RHS. 如果!isRunning为false,则第一个&&短路,并被评估为其LHS(false),而不评估RHS。

If !isRunning is true, the first && doesn't short circuit and is evaluated as its RHS ( id==="break-increment" && breakvar<=59 ) 如果!isRunning为true,则第一个&&不会短路,并被评估为其RHS( id==="break-increment" && breakvar<=59

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

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