简体   繁体   English

使用'&&'作为if语句的简写?

[英]Using '&&' as a shorthand if statement?

While I was killing time looking up Javascript shorthand patterns, I came across an interesting way of expressing an if statement here .当我在查找 Javascript 速记模式消磨时间时,我在这里遇到了一种表达if语句的有趣方式。 The following works:以下作品:

​var var1 = 5;
var var2 = 1;

var1 == 5 && var2++;

I think that's a totally cool (and shorter, cleaner) way of writing an if statement that only needs to do one operation.我认为这是编写只需要执行一个操作的if语句的一种非常酷(而且更短、更清晰)的方法。 However, I ran into an issue with the following code as soon as I tried something else with it:但是,当我尝试使用其他代码时,我遇到了以下代码的问题:

​var var1 = 5;
var var2 = 1;

var1 == 5 && var2 = 2;

Instead of working like the first code snippet, my test throws an error:我的测试没有像第一个代码片段那样工作,而是抛出了一个错误:

Uncaught ReferenceError: Invalid left-hand side in assignment未捕获的 ReferenceError:分配中的左侧无效

Why doesn't this work, and why is the left-hand side of my statement being called out as incorrect as opposed to the right hand?为什么这行不通,为什么我的陈述的左手边被认为是不正确的,而不是右手边?

This doesn't work because the && operator has higher precedence than the = operator . 这不起作用,因为&&运算符的优先级高于=运算符 In other words, your code is interpreted like this: 换句话说,您的代码解释如下:

(var1 == 5 && var2) = 2; 

If you manually put parentheses around the assignment, it will be interpreted correctly. 如果在赋值周围手动放置括号,则会正确解释它。

var1 == 5 && (var2 = 2); 

Other people will reprimand you for writing code like this, saying that is a bad idea because it makes the intent of the code harder to read--which is true, especially for people unfamiliar with this idiom--but it is important to try it out and experiment with things like this to see for yourself what happens. 其他人会谴责你写这样的代码,说这是一个坏主意,因为它使代码的意图更难阅读 - 这是真的,特别是对于不熟悉这个习语的人 - 但重要的是尝试它出去尝试这样的事情,亲眼看看会发生什么。 You've already encountered one of the annoying things about this approach. 你已经遇到过这种方法的烦人之处。

Anyway, in cases like this, I personally prefer to use single line if statements, like this 无论如何,在这种情况下,我个人更喜欢使用单行if语句,就像这样

if(condition) statement;

Although I'm sure others will tell you that that's bad style because you have to manually add brackets when you need more statements, this hasn't really bit me and IMO it's cleaner than using three lines just for a simple condition, like you said. 虽然我确信其他人会告诉你那种不好的风格,因为你需要在需要更多语句时手动添加括号,这对我来说并不是真的比IMO更简洁,只是为了一个简单的条件使用三行,就像你说的那样。

Don't be alarmed that this doesn't work as you'd expect, in fact less than a month ago Brendan Eich (Creator of JavaScript) was sharing with us that this type of use is better known as an "abusage" of the language, since this logical operator isn't meant to execute code like this, but rather it's meant to determine the value of an expression. 不要担心这不会像你期望的那样起作用,事实上不到一个月前,Brendan Eich(JavaScript的创造者)与我们分享这种类型的使用更为人所知的是“滥用”语言,因为这个逻辑运算符并不意味着执行这样的代码,而是意味着确定表达式的值。

"I also agree...that the && line is an abusage, allowed due to JS's C heritage by way of Java, but frowned upon by most JS hackers; and that an if statement would be much better style..." http://brendaneich.com/2012/04/the-infernal-semicolon/ “我也同意...... && line是一种滥用,由于JS通过Java的C传承而被允许,但大多数JS黑客都不赞成;而且if语句会更好......” http: //brendaneich.com/2012/04/the-infernal-semicolon/

That being said, you can avoid the operator precedence issue by wrapping your statements: 话虽如此,您可以通过包装语句来避免运算符优先级问题:

(var1 == 5) && (var2 = 2)

because you are using an invalid operator on the right-hand side. 因为您在右侧使用了无效的运算符。 its trying to read the entire statement as a single variable. 它试图将整个语句作为单个变量读取。

Or You could try using the ternary operator to make your code look concise.或者您可以尝试使用三元运算符使您的代码看起来简洁。

Is

var1 == 5 && var2++;

an cleaner or more understandable than this?比这更干净或更容易理解?

var2 += var1 === 5 ? 1 : 0;

In addition, the above expresses intent far more clearly than your code: the next guy who has to look at your code will see it and understand immediately what's going on, but looking at your code:此外,以上内容比您的代码更清楚地表达了意图:下一个必须查看您的代码的人会看到它并立即理解发生了什么,但是查看您的代码:

var1 == 5 && var2++

Is going to raise red flags in her head, because it looks like a bug or a some sort of typo — which means she's going to have to spend time trying to understand what you were actually trying to accomplish and whether or not the wonky code was intentional or not.会在她的脑海中发出危险信号,因为它看起来像一个错误或某种打字错误——这意味着她将不得不花时间试图理解你实际上想要完成什么,以及这个不稳定的代码是否是错误的有意无意。

My first reaction, looking at我的第一反应是看

var1 == 5 && var2++ ;

Is that an assignment was intended:这是一项任务吗:

var1 = 5 && var2++ ;

But the developer bounced the = key by accident, turning it into an orphaned expression (that happens to have side-effects).但是开发人员不小心弹回了=键,将其变成了一个孤立的表达式(恰好有副作用)。

Further, orphan expressions like that is likely to get flagged by any decent linter, for instance eslint's no-unused-expressions rule, as they indicate a probable bug.此外,像这样的孤立表达式可能会被任何体面的 linter 标记,例如 eslint 的no-unused-expressions规则,因为它们表明可能存在错误。

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

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