真值和假值的相等性 (JavaScript) [英] Equality of truthy and falsy values (JavaScript)

查看:20
本文介绍了真值和假值的相等性 (JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些对口译员来说似乎不一致的事情,尽管我知道这可能是有道理的,但我就是不明白.它与评估真/假值和布尔值的相等性有关.

示例 1:

if (document.getElementById('header')) {//在这里运行代码}

如果在文档中找到 id 为 'header' 的元素,则条件为真,因为对象的存在被认为是真实的.

示例 2:

if (document.getElementById('header) == true) {//在这里运行代码}

假设引用的元素在文档中找到.有人向我解释说,此条件将评估为 false,因为真值不等于布尔值 true.

这似乎没有意义.由于类型强制,对象的存在被认为是真实的,因此即使数据类型不同,它也应该等于真实.

考虑以下几点:

(false == 0)//计算结果为 true(false === 0)//计算结果为 false

当您使用等于运算符时,这种情况下 false 等于 0 为 true.因为 0 被认为是一个假值,所以它等于布尔值 false.值相同,数据类型不同.

对我来说,(document.getElementById('header') == true) 和 (false == 0) 是一回事.然而,他们都评估不同的东西.

有人可以向我解释为什么会这样吗?我一直在阅读对此的不同描述,但似乎没有人深入解释它.

解决方案

document.getElementById('header') 返回一个 DOM 对象或 null.因此,在您的第二次比较中(假设它正在解析为一个对象),您正在比较:

if (obj == true)

DOM 对象不是 ==true.

要了解原因,您必须查看 ECMAScript 自动类型转换规则,您可以在此处的 ECMAScript 5 规范中看到:http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3.

比较 object == boolean 时的操作规则一直到规则 9 和 10.

<块引用>

  1. 如果 Type(x) 是 Object 并且 Type(y) 是 String 或 Number,则返回比较结果 ToPrimitive(x) == y.

  2. 返回 false.

第 9 条规则是第一个规则,其中 Type(x)Object 并且两个类型不相同,所以它是第一个检查的.

由于 Type(y)Boolean,它没有通过规则 9.

因此,当它没有通过任何规则 1-9 时,它的计算结果为 false.

<小时>

当您进行此比较时:

(false == 0)//计算结果为 true

您正在查看类型转换规则中的规则 #6:

ToNumber(false) == 0

将解析为:

0 == 0

这将是 true.

<小时>

对于这个:

(false === 0)

每当您使用 === 时,如果两种类型不同,则结果立即为 false,因此计算结果为 false.=== 要求类型 AND 值相同,因此如果类型不同,则它甚至不会尝试比较该值.=== 从来没有进行过类型转换,这是使用它的有用原因之一.

<小时>

您应该了解 truthyfalsey 规则仅适用于像这样没有 === 的单个检查== 在比较中:

if (obj)

当您比较 x == y 时,它们不适用.为此,您必须参考规范第 11.9.3 节中的类型转换规则(上面链接).

I've encountered something which seems inconsistent on the part of the interpreter, though I know it probably makes sense and I just don't understand it. It has to do with evaluating the equality of a truthy/falsy values and Booleans.

Example 1:

if (document.getElementById('header')) {
//run code here
}

If the element with an id of 'header' is found in the document, the condition is true because the presence of an object is considered truthy.

Example 2:

if (document.getElementById('header) == true) {
// run code here
}

Presuppose the referenced element is found within the document. It was explained to me that this condition will evaluate to false because a truthy value does not equal a Boolean value of true.

This doesn't seem to make sense. The presence of the object is considered truthy due to type coercion, so therefore it should equal true even though the data types are different.

Consider the following:

(false == 0) // evaluates to true
(false === 0) // evaluates to false

This is a case where false equals 0 is true when you use the equals to operator. Because 0 is considered a falsy value, it is equal to the Boolean value of false. The values are the same and the data types are different.

To me, (document.getElementById('header') == true) and (false == 0) are the same thing. And yet, they both evaluate to something different.

Can someone please explain to me why this is the case? I've been reading different descriptions of this, but no one seems to explain it in-depth.

解决方案

document.getElementById('header') returns a DOM object or null. So, in your second comparison (assuming it is resolving to an object), you're comparing:

if (obj == true)

A DOM object is not == to true.

To see why, you have to go to the ECMAScript rules for automatic type conversion which you can see in the ECMAScript 5 specification here: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3.

The operative rules when comparing an object == boolean are all the way down to rules 9 and 10.

  1. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.

  2. Return false.

Rule 9 is the first rule where Type(x) is Object and the two types are not the same, so it's the first one to check.

Since Type(y) is Boolean, it does not pass rule 9.

Thus, when it hasn't passed any rules 1-9, it evaluates to false.


When you do this comparison:

(false == 0) // evaluates to true

you're looking at rule #6 in the type conversion rules which will do:

ToNumber(false) == 0

which will resolve to:

0 == 0

which will be true.


For this one:

(false === 0)

Whenever you use ===, if the two types are different, then the result is immediately false so this evaluates to false. === requires type AND value to be the same so if the type is not the same, then it doesn't even attempt to compare the value. No type conversion is ever done with === which is one of the useful reasons to use it.


You should understand that the truthy and falsey rules apply to only a single check like this with no == or === in the comparison:

if (obj)

They do not apply when you are comparing x == y. For that, you have to refer to the type conversion rules in section 11.9.3 of the specification (linked above).

这篇关于真值和假值的相等性 (JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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