null与undefined及其在JavaScript中的行为 [英] null vs. undefined and their behaviour in JavaScript
问题描述
因此,在javascript中对null和undefined的实现进行了大量争论/辩论/讨论后,我想让某人解释实现背后的原因以及它们在某些情况下的不同之处。我觉得麻烦的一些特殊要点:
So after a big argument/debate/discussion on the implementation of null and undefined in javascript I'd like somebody to explain the reasoning behind the implementation and why they differ in some circumstances. Some particular points I find troubling:
-
null == undefined
评估为true
-
null + 1
等于1但undefined + 1
等于NaN
-
if(!null)
计算结果为true,if(null)
计算结果为false,但null == false
计算结果为false。
null == undefined
evaluates totrue
null + 1
equals 1 butundefined + 1
equalNaN
if(!null)
evaluates to true andif(null)
evaluates to false butnull == false
evaluates to false.
我已经阅读了规范并且我知道结果是如何达到的,我正在寻找范式和原因来决定这是什么规格。其中一些,特别是第二个,给出第一个,感觉非常不一致。
I've read the specification and I know how the results are reached, I'm looking for the paradigms and reasons that dictate this being the specification. Some of these points, especially the second one, given the first, feel very inconsistent.
推荐答案
短而甜的版本是那个JavaScript是由Netscape团队非常迅速地设计和实现的,它有一些不一致的地方,比如你已经指出的那些。
The short and sweet version is that JavaScript was designed and implemented very rapidly by the Netscape team, and it had some inconsistencies such as the ones that you've pointed out.
Internet Exploder团队做得最好复制JS 正好,他们做得非常好,以至于不一致也被复制了。当Netscape将JS标准化时,因为ECMAScript MS是其中的一部分,并且基本上说他们不允许更改标准,因为它会破坏旧代码(现有系统惯性)。不一致是标准化的,就是这样。
The Internet Exploder team did its best to copy JS exactly and they did a damn good job of it to the point that the inconsistencies were copied as well. When Netscape went to get JS standardized as ECMAScript MS was a part of it and basically said that they weren't allowed to change the standard because it would break old code (existing systems inertia). The inconsistencies were standardized and that was that.
Douglas Crockford a关于其中一些问题的一系列非常好的讨论。
Douglas Crockford has a very good series of talks about some of these issues.
这篇关于null与undefined及其在JavaScript中的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!