<tfoot id='77tHN'></tfoot><legend id='77tHN'><style id='77tHN'><dir id='77tHN'><q id='77tHN'></q></dir></style></legend>

        <bdo id='77tHN'></bdo><ul id='77tHN'></ul>

    1. <i id='77tHN'><tr id='77tHN'><dt id='77tHN'><q id='77tHN'><span id='77tHN'><b id='77tHN'><form id='77tHN'><ins id='77tHN'></ins><ul id='77tHN'></ul><sub id='77tHN'></sub></form><legend id='77tHN'></legend><bdo id='77tHN'><pre id='77tHN'><center id='77tHN'></center></pre></bdo></b><th id='77tHN'></th></span></q></dt></tr></i><div id='77tHN'><tfoot id='77tHN'></tfoot><dl id='77tHN'><fieldset id='77tHN'></fieldset></dl></div>
      1. <small id='77tHN'></small><noframes id='77tHN'>

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

        时间:2023-09-04

        <legend id='QChUN'><style id='QChUN'><dir id='QChUN'><q id='QChUN'></q></dir></style></legend>
            <tbody id='QChUN'></tbody>
          • <small id='QChUN'></small><noframes id='QChUN'>

              1. <tfoot id='QChUN'></tfoot>
                <i id='QChUN'><tr id='QChUN'><dt id='QChUN'><q id='QChUN'><span id='QChUN'><b id='QChUN'><form id='QChUN'><ins id='QChUN'></ins><ul id='QChUN'></ul><sub id='QChUN'></sub></form><legend id='QChUN'></legend><bdo id='QChUN'><pre id='QChUN'><center id='QChUN'></center></pre></bdo></b><th id='QChUN'></th></span></q></dt></tr></i><div id='QChUN'><tfoot id='QChUN'></tfoot><dl id='QChUN'><fieldset id='QChUN'></fieldset></dl></div>
                  <bdo id='QChUN'></bdo><ul id='QChUN'></ul>
                  本文介绍了真值和假值的相等性 (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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 if([]) 被验证而 [] == false 在javascript中? 下一篇:转换字符串“true"/“假"为布尔值

                  相关文章

                • <legend id='BwM0M'><style id='BwM0M'><dir id='BwM0M'><q id='BwM0M'></q></dir></style></legend>

                  <small id='BwM0M'></small><noframes id='BwM0M'>

                    <bdo id='BwM0M'></bdo><ul id='BwM0M'></ul>
                • <i id='BwM0M'><tr id='BwM0M'><dt id='BwM0M'><q id='BwM0M'><span id='BwM0M'><b id='BwM0M'><form id='BwM0M'><ins id='BwM0M'></ins><ul id='BwM0M'></ul><sub id='BwM0M'></sub></form><legend id='BwM0M'></legend><bdo id='BwM0M'><pre id='BwM0M'><center id='BwM0M'></center></pre></bdo></b><th id='BwM0M'></th></span></q></dt></tr></i><div id='BwM0M'><tfoot id='BwM0M'></tfoot><dl id='BwM0M'><fieldset id='BwM0M'></fieldset></dl></div>

                    1. <tfoot id='BwM0M'></tfoot>