javascript 哪个逻辑运算符优先
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11157814/
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
Which Logic Operator Takes Precedence
提问by JTApps
So, I'm looking into writing a slightly more complex operation with logic operators in an if-else statement. I know I can do parentheses, and I know it's the better way of doing this, but I've gotten curious and so I'm going to ask. If I were to do something like this:
因此,我正在考虑在 if-else 语句中使用逻辑运算符编写稍微复杂的操作。我知道我可以做括号,而且我知道这是更好的方法,但我很好奇,所以我要问。如果我要做这样的事情:
if (firstRun == true || selectedCategory != undefined && selectedState != undefined) {
//Do something
} else {
//Do something else
}
How will that be operated without the use of parentheses? I know there is an order of operations for logic operators, similar to PEMDAS, right? I'm curious if it'll be ran something like this:
如何在不使用括号的情况下进行操作?我知道逻辑运算符有一个操作顺序,类似于 PEMDAS,对吗?我很好奇它是否会像这样运行:
firstRun == true || (selectedCategory != undefined && selectedState != undefined)
or maybe if the 'OR' operator takes precedence instead and it ends up going like:
或者,如果 'OR' 运算符优先,它最终会变成这样:
(firstRun == true || selectedCategory != undefined) && selectedState != undefined
The full list would be nice, if you can find it somewhere, of the order of operations for this. Thanks!
完整的列表会很好,如果你能在某处找到它,它的操作顺序。谢谢!
回答by Christoph
My rule of thumb, which covers basically 99% of all use cases for conditional statements, is:
我的经验法则,基本上涵盖了条件语句所有用例的 99%,是:
- Grouping:
()
- Member access
. or [...]
- Not:
!
- Comparison, e.g.
< , >= , === , !=, ...
- Logical AND
&&
- Logical OR
||
- 分组:
()
- 会员访问
. or [...]
- 不是:
!
- 比较,例如
< , >= , === , !=, ...
- 逻辑与
&&
- 逻辑或
||
MDN gives you the exhaustive breakdown: Javascript Operator Precedence
MDN 为您提供详尽的分类:Javascript 运算符优先级
so for your example:
所以对于你的例子:
(firstRun == true || selectedCategory != undefined && selectedState != undefined)
equals
等于
(firstRun == true) || ((selectedCategory != undefined) && (selectedState != undefined))
For anything more complex than the above mentioned cases I would look into refactoring the code for readabilities sake anyways!
对于比上述情况更复杂的任何事情,我都会考虑重构代码以提高可读性!
回答by Michal Leszczyk
There is a pretty good rule of thumb to this. Think of these operators as of mathematical ones:
对此有一个很好的经验法则。将这些运算符视为数学运算符:
AND
is multiplication (eg.0 * 1 = 0 => FALSE
)OR
is adding (eg.0 + 1 = 1 => TRUE
)
AND
是乘法(例如。0 * 1 = 0 => FALSE
)OR
正在添加(例如0 + 1 = 1 => TRUE
)
When you remember this, all you have to know is that multiplication always comes beforeaddition.
当你记住这一点时,你只需要知道乘法总是在加法之前。
回答by Aaron Digulla
See this chart for precedence.
请参阅此图表以了解优先级。
I'm not going to explain what happens because the next guy reading your code will think: "WTF? Does that do what it should?"
我不打算解释会发生什么,因为下一个阅读你的代码的人会想:“WTF?它做了它应该做的吗?”
So the better solution is to wrap the terms in parentheses even if you know the precedence, applied it correctly andthe code works
所以更好的解决方案是将术语括在括号中,即使您知道优先级,正确应用它并且代码工作
This follows the old wisdom that you shouldn't do everything you can just because you can do it. Always keep an eye on the consequences.
这遵循古老的智慧,你不应该仅仅因为你能做到就去做你能做的一切。始终关注后果。
回答by Blindy
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence
&&
is before ||
, so your expression is equivalent to:
&&
是 before ||
,所以你的表达式等价于:
firstRun == true || (selectedCategory != undefined && selectedState != undefined)
回答by Mariy
It will be the first:
这将是第一个:
firstRun == true || (selectedCategory != undefined && selectedState != undefined)
As a general rule in most programming languages AND has higher precedence
作为大多数编程语言的一般规则并且具有更高的优先级
回答by pete
While Logical Operator Precedence is not actually defined in the ECMAScript Specification, MDNdoes a pretty good job of it and even has a separate page for Logical Operators.
虽然ECMAScript 规范中实际上没有定义逻辑运算符优先级,但MDN做得很好,甚至有一个单独的逻辑运算符页面。
My concern I suppose, since Logical Operator Precedence is not actually defined in the ECMAScript Specification, each individual browser vendor can potentially be different (I'm talking to you, Internet Explorer!) so YMMV.
我想我担心的是,由于 ECMAScript 规范中实际上并未定义逻辑运算符优先级,因此每个浏览器供应商都可能会有所不同(我正在与您交谈,Internet Explorer!)所以 YMMV。
In the event anyone wants to test this across different browsers, here's a test case fiddle: http://jsfiddle.net/HdzXq/
如果有人想在不同的浏览器上测试这个,这里有一个测试用例:http: //jsfiddle.net/HdzXq/
回答by Mitchell
I know this is an old post, but I was wondering if the order should be reversed. I've always thought that the more demanding half of an or statement should be listed first, because it's less likely to pass:
我知道这是一个旧帖子,但我想知道是否应该颠倒顺序。我一直认为 or 语句中要求更高的一半应该首先列出,因为它不太可能通过:
The correct response from Cristoph:
克里斯托夫的正确回答:
(firstRun == true) || ((selectedCategory != undefined) && (selectedState != undefined))
(firstRun == true) || ((selectedCategory != undefined) && (selectedState != undefined))
Should it actually be written as?:
它实际上应该写为?:
((selectedCategory != undefined) && (selectedState != undefined) || firstRun == true))
((selectedCategory != undefined) && (selectedState != undefined) || firstRun == true))
I feel like there's a good chance that you would never even get to the second half in the first example.
我觉得在第一个例子中你很有可能永远不会进入下半场。
Let me know what you think.
让我知道你的想法。