我在我的书《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.
发布评论
评论(4)
需要注意的一个重要区别是
null
的Type
是空
。(忽略
typeof
它返回"object"
fornull
因为糟糕的设计和向后兼容性)ES5 规范
表示与
null 的比较
和Boolean
应该返回 false,因为Null
和Boolean
的Type
不同,并且 11.9.3 中的其他步骤均不适用,因此会发生return false
默认操作x
的Type
和 < code>y 不同,x
或y
为null
,但==
操作仍然返回true
为这意味着
undefined == null
返回true
特别注意:
有一个 ES6:harmony 提案 修复
typeof null
An important distinction to make is that the
Type
ofnull
isNull
.(ignore
typeof
it returns"object"
fornull
because of bad design and backwards compatibility)The ES5 specification
Says that the comparison with
null
and aBoolean
should return false because theType
ofNull
andBoolean
are not the same, and none of the other steps in 11.9.3 apply so the default action ofreturn false
happensThe only case where the
Type
ofx
andy
are different and eitherx
ory
isnull
but the==
operation still returnstrue
areThat means
undefined == null
returnstrue
Of special note:
There is an ES6:harmony proposal to fix
typeof null
实际上我认为他指的是
typeof null == 'object'
的事实,不幸的是事实确实如此。但这是 typeof 运算符的特性,而不是 null 本身的特性。 Null 是虚假值,但根据规范, typeof 返回“object”: http://bclary.com/2004/11/07/#a-11.4.3Actually 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当您将
null
与true
进行比较时,它的计算结果为因此它不等于false
,true< /代码>。同样,当在任何其他必须将其视为布尔值的上下文中使用时(例如
if
或while
表达式),它是false
。说“null 是类型对象”并不正确,因为它不是。它是
null
。它实际上没有任何类型,(感谢@Roee Gavirel)它有自己的类型(空类型),因为它什么都不是。换句话说,如果一个变量的值为null
,则意味着它没有引用任何内容;根本没有对象。编辑 - 等一下,因为我的大脑还在睡觉。
好的,这就是规范中的内容。如果
==
的操作数之一是布尔值,则该布尔值将转换为数字(确实如此),并且转换将以这种方式进行。这就是为什么null
既不是true
也不是false
的==
。因此,“奇怪”与其说是关于null
,不如说是关于评估==
比较的复杂规则。ECMA-262 标准的第 11.9.3 节以或多或少可以理解的方式详细说明了这一点。可以说
==
的语义一点也不简单。When you compare
null
totrue
,it's evaluated asso it's not equal tofalse
,true
. Similarly, when used in any other context where it must be treated as a boolean — like anif
orwhile
expression — it'sfalse
.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 valuenull
, 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 whynull
is==
to neithertrue
norfalse
. The "strangeness", therefore, is not so much aboutnull
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.我不明白问题出在哪里...
如果 (null == true) = false 那么 (null == false) = true。
如果问题是“为什么”,那么答案就是易用性。 (可能取自 C)但如果你想知道引用是否有效,你可以这样做:
而不是:
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:
Rather then: