JavaScript 有非短路布尔运算符吗?
在 JavaScript 中,
(f1() || f2())
如果 f1
返回 true
,则不会执行 f2
,这通常是一件好事,除非它不是。有没有不短路的||
版本?
像这样的东西
var or = function(f, g){var a = f(); var b = g(); return a||b;}
In JavaScript
(f1() || f2())
won't execute f2
if f1
returns true
which is usually a good thing except for when it isn't. Is there a version of ||
that doesn't short circuit?
Something like
var or = function(f, g){var a = f(); var b = g(); return a||b;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,JavaScript 不像 Java,唯一的逻辑运算符是短路的
https ://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators
也许这可以帮助你:
http://cdmckay.org/blog/2010/09/09/eager-boolean-operators-in-javascript/
基本上
(a && b)
是短路的,而!!(a + b)
不是短路的,并且它们产生相同的值。Nope, JavaScript is not like Java and the only logical operators are the short-circuited
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators
Maybe this could help you:
http://cdmckay.org/blog/2010/09/09/eager-boolean-operators-in-javascript/
Basically
(a && b)
is short-circuiting while!!(a + b)
is not and they produce the same value.只要您的函数返回布尔值,您就可以使用按位或(或者这真的很重要吗?):
我在这里玩过这个:http://jsfiddle.net/sadkinson/E9eWD/1/
You could use bit-wise OR as long as your functions return boolean values (or would that really matter?):
I played with this here: http://jsfiddle.net/sadkinson/E9eWD/1/
JavaScript 确实有非短路的单管道(
|
,按位 OR)和单与号运算符(&
,按位 AND),但同样,它们是按位的,而不是合乎逻辑的。http://www.eecs.umich.edu/~bartlett/jsops.html
JavaScript DOES have single pipe (
|
, bitwise OR) and single ampersand operators (&
, bitwise AND) that are non-short circuiting, but again they are bitwise, not logical.http://www.eecs.umich.edu/~bartlett/jsops.html
如果您需要 f2() 运行,无论 f1() 是真还是假,您应该简单地调用它,返回一个布尔变量,并在条件中使用它。也就是说,使用:
if (f1() || f2IsTrue)
否则,按照 GregC 的建议使用单个横杠或单个 & 符号。
If you need f2() to run regardless of whether or not f1() is true or false, you should simply be calling it, returning a boolean variable, and using that in your conditional. That is, use:
if (f1() || f2IsTrue)
Otherwise, use single bar or single ampersand as suggested by GregC.
非短路和:
[f1(), f2()].every(i => i)
非短路或:< code>[f1(), f2()].some(i => i)
要在 Chrome 控制台中测试非短路和,请执行以下命令:
结果:
要测试 >非短路或在 Chrome 控制台中,执行以下命令:
结果:
方括号语法称为数组文字。该数组填充了函数
f1
和f2
的返回值,这两个函数都被求值。然后以每个数组元素作为操作数进行逻辑运算。逻辑运算本身是短路的,但这并不重要,因为操作数已经被评估。因此,整个结构是非短路的。文档:
https://developer.mozilla.org/ en-US/docs/Web/JavaScript/Guide/Grammar_and_types#array_literals
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
https://developer.mozilla.org/en-US/docs/Web/JavaScript/参考/Global_Objects/Array/some
Non-shortcircuiting and:
[f1(), f2()].every(i => i)
Non-shortcircuiting or:
[f1(), f2()].some(i => i)
To test non-shortcircuiting and in Chrome console, exectute this:
Result:
To test non-shortcircuiting or in Chrome console, exectute this:
Result:
The square bracket syntax is called array literal. The array is filled with the return values of the functions
f1
andf2
, which are both evaluated. Then the logic operation is done with each array element as an operand. The logic operations themselves are shortcircuiting, but that does not matter because the operands have already been evaluated. So the construct as a whole is non-shortcircuiting.Documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#array_literals
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
&
和|
是按位的,但如果输入是布尔值,您通常可以忽略结果为 0 或 1 的事实:如果您需要布尔结果,请使用“double not”分别将 0 和 1 转换为
false
和true
:如果输入不是布尔值并且您希望与处理
的 JavaScript 方式兼容0 ,
-0
、NaN
、undefined
、null
asfalse
:这些可以缩短使用德摩根定律:
尽管“双重不”对我来说读起来更好。
&
and|
are bitwise, but if the inputs are booleans, you can often ignore the fact that the result is 0 or 1:If you need a boolean result, use "double not" to convert 0 and 1 to
false
andtrue
respectively:If the inputs aren't booleans and you want to be compatible with JavaScript way of treating
0
,-0
,NaN
,undefined
,null
asfalse
:These can be shortened using De Morgan's laws to:
although the "double not" reads better to me.