Null 是类型对象,所以它是真的?幕后发生了什么?

发布于 2024-12-07 18:42:12 字数 300 浏览 1 评论 0 原文

我在我的书《Elegant JavaScript》中读到,null == true 的计算结果为 false。使用解释器,我已经确认这是TRUE。然而,在本章后面(事实上,在同一页上),它说当 null 作为 if、while 或 for 语句的条件给出时,它将转换为布尔值并返回 false。

有更深入了解的人可以告诉我这是为什么吗?我知道在哪里可以找到浏览器源代码,但我不确定如何定位导致这种特殊且不直观行为的编程。因为我对 C++ 知之甚少,所以我也很感激任何有关独立查找此类信息的提示。

谢谢。

I'm reading in my book, "Elegant JavaScript", that null == true evaluates as false. Using an interpreter, I have confirmed this to be TRUE. However, later in the chapter--in fact, on the same page--, it says that when null is given as the condition of an if, while, or for statement, it will be converted to a Boolean and return false.

Can anybody with a deeper insight tell me why this is? I know where to find browser source code, but I'm not sure how to target the programming that is responsible for this peculiar and unintuive behavior. Because I know very little C++, I would also appreciate any tips on finding info like this, independently.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

極樂鬼 2024-12-14 18:42:12

需要注意的一个重要区别是 nullType

(忽略 typeof 它返回 "object" for null 因为糟糕的设计和向后兼容性)

11.9.3 抽象相等比较算法 # Ⓣ 比较
x == y(其中 x 和 y 是值)产生 true 或 false。这样一个
比较如下:

[...剥离]

  • 返回错误。
  • ES5 规范

    表示与 null 的比较Boolean 应该返回 false,因为 NullBooleanType 不同,并且 11.9.3 中的其他步骤均不适用,因此会发生 return false 默认操作

    xType 和 < code>y 不同,xynull,但 == 操作仍然返回 true

    如果 x 为 null 并且 y 未定义,则返回 true。

    如果 x 未定义且 y 为 null,则返回 true。

    这意味着 undefined == null 返回 true

    特别注意:

    有一个 ES6:harmony 提案 修复 typeof null

    An important distinction to make is that the Type of null is Null.

    (ignore typeof it returns "object" for null because of bad design and backwards compatibility)

    11.9.3 The Abstract Equality Comparison Algorithm # Ⓣ The comparison
    x == y, where x and y are values, produces true or false. Such a
    comparison is performed as follows:

    [... stripped]

    1. Return false.

    The ES5 specification

    Says that the comparison with null and a Boolean should return false because the Type of Null and Boolean are not the same, and none of the other steps in 11.9.3 apply so the default action of return false happens

    The only case where the Type of x and y are different and either x or y is null but the == operation still returns true are

    If x is null and y is undefined, return true.

    If x is undefined and y is null, return true.

    That means undefined == null returns true

    Of special note:

    There is an ES6:harmony proposal to fix typeof null

    沉默的熊 2024-12-14 18:42:12

    实际上我认为他指的是 typeof null == 'object' 的事实,不幸的是事实确实如此。但这是 typeof 运算符的特性,而不是 null 本身的特性。 Null 是虚假值,但根据规范, typeof 返回“object”: http://bclary.com/2004/11/07/#a-11.4.3

    Actually I think he refers to the fact that typeof null == 'object', which is unfortunately the case. But that's a peculiarity of the typeof operator, not of null itself. Null is falsy value, but typeof returns "object" for it, according to the spec: http://bclary.com/2004/11/07/#a-11.4.3

    墟烟 2024-12-14 18:42:12

    当您将 nulltrue 进行比较时,它的计算结果为 false 因此它不等于 true< /代码>。同样,当在任何其他必须将其视为布尔值的上下文中使用时(例如 ifwhile 表达式),它是 false

    说“null 是类型对象”并不正确,因为它不是。它是null它实际上没有任何类型,(感谢@Roee Gavirel)它有自己的类型(空类型),因为它什么都不是。换句话说,如果一个变量的值为 null,则意味着它没有引用任何内容;根本没有对象。

    编辑 - 等一下,因为我的大脑还在睡觉。

    好的,这就是规范中的内容。如果 == 的操作数之一是布尔值,则该布尔值将转换为数字(确实如此),并且转换将以这种方式进行。这就是为什么 null 既不是 true 也不是 false==。因此,“奇怪”与其说是关于 null,不如说是关于评估 == 比较的复杂规则。

    ECMA-262 标准的第 11.9.3 节以或多或少可以理解的方式详细说明了这一点。可以说 == 的语义一点也不简单。

    When you compare null to true, it's evaluated as false, so it's not equal to true. Similarly, when used in any other context where it must be treated as a boolean — like an if or while expression — it's false.

    It's not really correct to say that "null is type object", because it is not. It is null. It doesn't really have any type, (Thanks @Roee Gavirel) It's got it's own type (the null type) because it isn't anything. In other words, if a variable has the value null, that means that it is referring to nothing; no object at all.

    edit — crap hold on a sec because my brain's still asleep.

    OK here's what's in the spec. If one of the operands of == is boolean, then the boolean is converted to a number (yes really) and the conversion proceeds that way. That's why null is == to neither true nor false. The "strangeness", therefore, is not so much about null as it is about the complicated rules for evaluating == comparisons.

    Section 11.9.3 of the ECMA-262 standard spells it all out in a more-or-less understandable way. Suffice to say that the semantics of == are not at all simple.

    云淡风轻 2024-12-14 18:42:12

    我不明白问题出在哪里...
    如果 (null == true) = false 那么 (null == false) = true。

    如果问题是“为什么”,那么答案就是易用性。 (可能取自 C)但如果你想知道引用是否有效,你可以这样做:

    if (RefValue) {
        //bla bla bla
    }
    

    而不是:

    if (RefValue == null) {
        //bla bla bla
    }
    

    I don't see what the problem...
    if (null == true) = false then (null == false) = true.

    if "why" is the question, then the answer is ease of use. (probably taken from C) but if you wish to know if a reference is valid or not you can just do:

    if (RefValue) {
        //bla bla bla
    }
    

    Rather then:

    if (RefValue == null) {
        //bla bla bla
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文