JavaScript 有非短路布尔运算符吗?

发布于 2024-11-01 19:28:47 字数 279 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空袭的梦i 2024-11-08 19:28:48

不,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 | a * b     | a || b | a + b     |
|-------|-------|--------|-----------|--------|-----------|
| false | false | false  | 0         | false  | 0         |
| false | true  | false  | 0         | true   | 1         |
| true  | false | false  | 0         | true   | 1         |
| true  | true  | true   | 1         | true   | 2         |

| a     | b     | a && b | !!(a * b) | a || b | !!(a + b) |
|-------|-------|--------|-----------|--------|-----------|
| false | false | false  | false     | false  | false     |
| false | true  | false  | false     | true   | true      |
| true  | false | false  | false     | true   | true      |
| true  | true  | true   | true      | true   | true      |

基本上 (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/

| a     | b     | a && b | a * b     | a || b | a + b     |
|-------|-------|--------|-----------|--------|-----------|
| false | false | false  | 0         | false  | 0         |
| false | true  | false  | 0         | true   | 1         |
| true  | false | false  | 0         | true   | 1         |
| true  | true  | true   | 1         | true   | 2         |

| a     | b     | a && b | !!(a * b) | a || b | !!(a + b) |
|-------|-------|--------|-----------|--------|-----------|
| false | false | false  | false     | false  | false     |
| false | true  | false  | false     | true   | true      |
| true  | false | false  | false     | true   | true      |
| true  | true  | true   | true      | true   | true      |

Basically (a && b) is short-circuiting while !!(a + b) is not and they produce the same value.

清晰传感 2024-11-08 19:28:48

只要您的函数返回布尔值,您就可以使用按位或(或者这真的很重要吗?):

if (f1() | f2()) {
    //...
}

我在这里玩过这个: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?):

if (f1() | f2()) {
    //...
}

I played with this here: http://jsfiddle.net/sadkinson/E9eWD/1/

国际总奸 2024-11-08 19:28:48

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

ζ澈沫 2024-11-08 19:28:48

如果您需要 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.

烟花肆意 2024-11-08 19:28:48

非短路和​​[f1(), f2()].every(i => i)

非短路或:< code>[f1(), f2()].some(i => i)

要在 Chrome 控制台中测试非短路和​​,请执行以下命令:

f1 = () => { console.log('evaluating f1'); return false; }
f2 = () => { console.log('evaluating f2'); return false; }
[f1(), f2()].every(i => i)

结果:

evaluating f1
evaluating f2
false

要测试 >非短路或在 Chrome 控制台中,执行以下命令:

f1 = () => { console.log('evaluating f1'); return true; }
f2 = () => { console.log('evaluating f2'); return true; }
[f1(), f2()].some(i => i)

结果:

evaluating f1
evaluating f2
true

方括号语法称为数组文字。该数组填充了函数 f1f2 的返回值,这两个函数都被求值。然后以每个数组元素作为操作数进行逻辑运算。逻辑运算本身是短路的,但这并不重要,因为操作数已经被评估。因此,整个结构是非短路的。

文档:

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:

f1 = () => { console.log('evaluating f1'); return false; }
f2 = () => { console.log('evaluating f2'); return false; }
[f1(), f2()].every(i => i)

Result:

evaluating f1
evaluating f2
false

To test non-shortcircuiting or in Chrome console, exectute this:

f1 = () => { console.log('evaluating f1'); return true; }
f2 = () => { console.log('evaluating f2'); return true; }
[f1(), f2()].some(i => i)

Result:

evaluating f1
evaluating f2
true

The square bracket syntax is called array literal. The array is filled with the return values of the functions f1 and f2, 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

娇女薄笑 2024-11-08 19:28:48

&| 是按位的,但如果输入是布尔值,您通常可以忽略结果为 0 或 1 的事实:

if (f1() | f2())

如果您需要布尔结果,请使用“double not”分别将 0 和 1 转换为 falsetrue

const orResult = !!(f1() | f2());

如果输入不是布尔值并且您希望与处理 的 JavaScript 方式兼容0 , -0NaNundefinednull as false

const andResult = !!(!!f1() & !!f2());
const orResult = !!(!!f1() | !!f2());

这些可以缩短使用德摩根定律

const andResult = !(!f1() | !f2());
const orResult = !(!f1() & !f2());

尽管“双重不”对我来说读起来更好。

& and | are bitwise, but if the inputs are booleans, you can often ignore the fact that the result is 0 or 1:

if (f1() | f2())

If you need a boolean result, use "double not" to convert 0 and 1 to false and true respectively:

const orResult = !!(f1() | f2());

If the inputs aren't booleans and you want to be compatible with JavaScript way of treating 0, -0, NaN, undefined, null as false:

const andResult = !!(!!f1() & !!f2());
const orResult = !!(!!f1() | !!f2());

These can be shortened using De Morgan's laws to:

const andResult = !(!f1() | !f2());
const orResult = !(!f1() & !f2());

although the "double not" reads better to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文