真值和假值的相等性 (JavaScript)

2023-09-04前端开发问题
1

本文介绍了真值和假值的相等性 (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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用百度地图API获取地理位置信息
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22 前端开发问题
244

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

正则表达式([A-Za-z])为啥可以匹配字母加数字或特殊符号?
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...
2024-06-06 前端开发问题
165