返回操作数之一的布尔运算符 [英] Boolean operators which return one of the operands
问题描述
在Python以及Javascript中,布尔值or
和and
运算符将返回其中一个操作数,而不是true
或false
.
In Python, and maybe in Javascript, the boolean or
and and
operators return one of the operands, instead of true
or false
.
- 在Python中,返回操作数之一:
'' || 'hello' == 'hello'
- 相比之下,在PHP中:
'' || 'hello' == true
;
- In Python, one of the operands is returned:
'' || 'hello' == 'hello'
- In comparison, in PHP:
'' || 'hello' == true
;
现在
- 如何调用布尔运算符的这种行为?
- 在所有浏览器中都可以使用Javascript吗?
推荐答案
为空合并运算符,&&
是空安全合并运算符( 要链接的链接,如果可以找到一个链接,抱歉,我找不到链接.
As Ignacio's answer points out, these are coalescing operators. ||
is the null coalescing operator, &&
is the null-safe coalescing operator (link to follow, if I can find one sorry, I can't find a link).
它们应该在所有浏览器中可用-它们都在ECMA-262第1,第2,第3和第5版中进行了定义,大多数当前的Javascript实现均基于第3或第5版.从ECMA-262第3版开始:
They should be available in all browsers - they are both defined in the ECMA-262 1st, 2nd, 3rd and 5th editions, most current Javascript implementations are based upon 3rd or 5th. From ECMA-262 3rd edition:
生产LogicalANDExpression:LogicalANDExpression&&按位计算ORExpression为 如下:
1.评估LogicalANDExpression.
2.调用GetValue(Result(1)).
3.调用ToBoolean(Result(2)).
4.如果Result(3)为false,则返回Result(2).
5.评估BitwiseORExpression.
6.调用GetValue(Result(5)).
7.返回结果(6).
The production LogicalANDExpression : LogicalANDExpression && BitwiseORExpression is evaluated as follows:
1. Evaluate LogicalANDExpression.
2. Call GetValue(Result(1)).
3. Call ToBoolean(Result(2)).
4. If Result(3) is false, return Result(2).
5. Evaluate BitwiseORExpression.
6. Call GetValue(Result(5)).
7. Return Result(6).
生产LogicalORExpression:LogicalORExpression || LogicalANDExpression计算为
如下:
1.评估LogicalORExpression.
2.调用GetValue(Result(1)).
3.调用ToBoolean(Result(2)).
4.如果Result(3)为true,则返回Result(2).
5.评估LogicalANDExpression.
6.调用GetValue(Result(5)).
7.返回结果(6).
The production LogicalORExpression : LogicalORExpression || LogicalANDExpression is evaluated as
follows:
1. Evaluate LogicalORExpression.
2. Call GetValue(Result(1)).
3. Call ToBoolean(Result(2)).
4. If Result(3) is true, return Result(2).
5. Evaluate LogicalANDExpression.
6. Call GetValue(Result(5)).
7. Return Result(6).
这篇关于返回操作数之一的布尔运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!