[英]Null is type object, so it's truthy? What's going on behind the scenes?
I'm reading in my book, "Elegant JavaScript", that null == true
evaluates as false. 我正在阅读我的书“优雅的JavaScript”,
null == true
评估为false。 Using an interpreter, I have confirmed this to be TRUE
. 使用口译员,我确认这是
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. 但是,在本章的后面 - 实际上,在同一页面上 - ,它表示当作为if,while或for语句的条件给出null时,它将被转换为布尔值并返回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.
因为我对C ++知之甚少,所以我也很欣赏任何有关如此独立查找信息的提示。
Thank you. 谢谢。
An important distinction to make is that the Type
of null
is Null
. 要做的一个重要区别是
null
Type
为Null
。
(ignore typeof
it returns "object"
for null
because of bad design and backwards compatibility) (忽略
typeof
因为设计错误和向后兼容性而返回null
"object"
)
11.9.3 The Abstract Equality Comparison Algorithm # Ⓣ The comparison x == y, where x and y are values, produces true or false.
11.9.3抽象等式比较算法#Ⓣ比较x == y,其中x和y是值,产生真或假。 Such a comparison is performed as follows:
这样的比较如下进行:
[... stripped]
[...剥离]
- Return false.
返回false。
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 说与
null
和Boolean
的比较应该返回false,因为Null
和Boolean
的Type
不相同,并且11.9.3中的其他步骤都不适用,因此return false
的默认操作发生
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 唯一的情况是
x
和y
的Type
不同, x
或y
为null
但==
操作仍然返回true
If x is null and y is undefined, return true.
如果x为null且y未定义,则返回true。
If x is undefined and y is null, return true.
如果x未定义且y为null,则返回true。
That means undefined == null
returns true
这意味着
undefined == null
返回true
Of special note: 特别说明:
There is an ES6:harmony proposal to fix typeof null
有一个ES6:和声提议来修复
typeof null
Actually I think he refers to the fact that typeof null == 'object'
, which is unfortunately the case. 实际上我认为他指的是
typeof null == 'object'
这一事实,遗憾的是这种情况。 But that's a peculiarity of the typeof operator, not of null itself. 但这是typeof运算符的特性,而不是null本身。 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是假值,但typeof为它返回“对象”,根据规范: http : //bclary.com/2004/11/07/#a-11.4.3
When you compare null
to true
,
it's evaluated as
so it's not equal to false
,
true
. 将
null
与true
进行比较时,
它被评估为
因此它不等于false
,
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
. 类似地,当在任何其他必须被视为布尔值的上下文中使用时 - 如
if
或while
表达式 - 它是false
。
It's not really correct to say that "null is type object", because it is not. 说“null是类型对象”是不正确的,因为它不是。 It is
null
. 它是
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.
它实际上没有任何类型,
(感谢@Roee Gavirel)它有自己的类型(null类型),因为它不是任何东西。 In other words, if a variable has the value null
, that means that it is referring to nothing; 换句话说,如果变量的值为
null
,则表示它没有引用; 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
. 这就是为什么
null
==
既不是true
也不是false
。 The "strangeness", therefore, is not so much about null
as it is about the complicated rules for evaluating ==
comparisons. 因此,“奇怪性”并不是关于
null
因为它是关于评估==
比较的复杂规则。
Section 11.9.3 of the ECMA-262 standard spells it all out in a more-or-less understandable way. ECMA-262标准的第11.9.3节以一种或多或少可理解的方式解释了它。 Suffice to say that the semantics of
==
are not at all simple. 可以说
==
的语义并不简单。
I don't see what the problem... 我看不出有什么问题......
if (null == true) = false then (null == false) = true. 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:
(可能取自C)但如果您想知道参考是否有效,您可以这样做:
if (RefValue) {
//bla bla bla
}
Rather then: 而不是:
if (RefValue == null) {
//bla bla bla
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.