做&&和||运营商将他们的操作数转换为布尔值? [英] Do the && and || operators convert their operands to booleans?

查看:148
本文介绍了做&&和||运营商将他们的操作数转换为布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Flanagan的O'Reilly JavaScript书中指出:

Flanagan's O'Reilly JavaScript book states:


与&&&和||经营者,!运算符将其操作数转换为
a布尔值[...],然后反转转换后的值。

Unlike the && and || operators, the ! operator converts its operand to a boolean value [...] before inverting the converted value.

如果那些逻辑运算符不要不将操作数转换为布尔值,如何评估表达式?

If those logical operators don't convert the operands to booleans, how can the expression be evaluated?

推荐答案

Per 规范,第11.11节:二进制逻辑运算符:

Per specification, section 11.11: Binary Logical Operators:

评估&& ] ......的产量评估如下:

The production [of evaluating &&] ... is evaluated as follows:


  1. 让lref成为评估LogicalANDExpression的结果。

  2. 让lval成为GetValue(lref)。

  3. 如果 ToBoolean(lval)为false,则返回lval。

  4. 让rref成为评估BitwiseORExpression的结果。

  5. 返回GetValue(rref)。

  1. Let lref be the result of evaluating LogicalANDExpression.
  2. Let lval be GetValue(lref).
  3. If ToBoolean(lval) is false, return lval.
  4. Let rref be the result of evaluating BitwiseORExpression.
  5. Return GetValue(rref).

生产[评估 || ] ...评估为如下:

The production [of evaluating ||] ... is evaluated as follows:


  1. 让lref成为评估LogicalORExpression的结果。

  2. 让lval成为GetValue(lref)。

  3. 如果 ToBoolean(lval)为true,则返回lval。

  4. 让rref成为评估LogicalANDExpression的结果。

  5. 返回GetValue(rref)。

  1. Let lref be the result of evaluating LogicalORExpression.
  2. Let lval be GetValue(lref).
  3. If ToBoolean(lval) is true, return lval.
  4. Let rref be the result of evaluating LogicalANDExpression.
  5. Return GetValue(rref).

所以内部 value是转换为布尔值。但是,因为这从未暴露过 - 整个语义解释是一个抽象,可以被优化出来 - && 和<的行为code> || 可以通过使用truthy-and-falsy值进行简单解释(其中 ToBoolean 涵盖:truthy-value是一个 ToBoolean 返回true,所有其他值都是假的。)

So internally the value is "converted to a boolean". However, since this is never exposed -- and the entire semantic explanation is an abstraction which can be/is "optimized out" -- the behavior of && and || can be simply explained through using truthy-and-falsy values (for which ToBoolean covers: a truthy-value is one for which ToBoolean returns true, all other values are falsy).

<$ c $的逻辑表c>&& 是:

a       b      result
truthy  any    b
falsy   any    a

|| 的逻辑表是:

a       b      result
truthy  any    a
falsy   any    b

请注意<$ c $的评估返回c> a 或 b

快乐编码。

为完整起见,请参阅第9.2节:

For completeness, from section 9.2:

抽象操作 ToBoolean 将其参数转换为ty值pe boolean as ...

The abstract operation ToBoolean converts its argument to a value of type boolean as ...


  • 未定义 - false

  • Null - false

  • boolean(不是Boolean对象!) - 结果等于输入参数(无转换)。

  • number(不是Number对象!) - 如果参数为+ 0,-0或NaN,则结果为false;否则结果为真。

  • string(不是String对象!) - 如果参数为空String(其长度为零),则结果为false;否则结果是真的。

  • 对象 - 真实

  • Undefined - false
  • Null - false
  • boolean (not Boolean object!) - The result equals the input argument (no conversion).
  • number (not Number object!) - The result is false if the argument is +0, -0, or NaN; otherwise the result is true.
  • string (not String object!) - The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
  • Object - true

这篇关于做&amp;&amp;和||运营商将他们的操作数转换为布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆