truthy和falsy价值的平等(JavaScript的) [英] Equality of truthy and falsy values (JavaScript)

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

问题描述

我遇到了一些东西,似乎在跨preTER部分不一致的,虽然我知道这可能是有道理的,我只是不明白。它与评估truthy / falsy值和布尔平等做。

实施例1:

 如果(的document.getElementById('头')){
//运行code在这里
}

如果用'头'的ID的元素在文档中被发现,因为对象的presence被认为是truthy条件为真。

例2:

 如果(的document.getElementById('头)==真){
//运行code在这里
}

presuppose引用的元素在文档中找到。有人向我解释,这种情况将评估为假,因为truthy值不等于一个布尔值。

这似乎并没有什么意义。对象的presence被认为是truthy由于类型转换,因此,它应该等于真正即使数据类型是不同的。

考虑以下几点:

 (假== 0)//计算结果为真
(假=== 0)//计算结果为假

这是错误的等于当您使用等于运营商0是真实的情况。因为0被认为是一个falsy值,它等于假的布尔值。的值是相同的,数据类型是不同的。

对于我来说,(的document.getElementById('头')==真)和(假== 0)是一样的。然而,他们都评价到不同的东西。

可有人请向我解释为什么是这样的呢?我一直在阅读的不同的描述,但似乎没有人来解释它深入。


解决方案

的document.getElementById('头')返回一个DOM对象或。所以,在你的第二个比较(假设它解析为一个对象),你就比较:

 如果(OBJ ==真)

一个DOM对象不是 == 真正

要明白为什么,你必须去ECMAScript的规则类型的自动转换,你可以在这里的ECMAScript 5规范看到:的 http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

比较一个对象时的

执行规则==布尔下跌规则9和10的所有道路。


  
  
  • 如果类型(x)是对象和类型(y)为String或号码,返回比较的结果ToPrimitive(x)的==年。


  •   
  • 返回FALSE。


  •   

    第9条的第一条规则,其中键入(X)对象和两种类型是不一样的,所以它要检查的第一个。

    由于输入(Y)布尔,它不通过第9条。

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


    当你这样做比较:

     (假== 0)//计算结果为真

    你的类型转换规则将做望着规则#6:

      ToNumber(假)== 0

    这将解析为:

      0 == 0

    这将是真正


    有关这一个:

     (假=== 0)

    当你使用 === ,如果两个类型是不同的,那么结果是立即等等这个计算结果为 === 要求类型和值是一样的,所以如果该类型是不一样的,那么它不甚至试图比较值的。没有类型转换曾以做 === 这是有用的理由使用它的。


    您应该明白, truthy falsey 规则适用于只有一个检查这样没有 == === 中的比较:

     如果(OBJ)

    当你比较他们不适用 X ==是。对于这一点,你必须参考的类型转换规则在规范(上面链接)第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).

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

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