为什么JavaScript中存在“null”值? [英] Why is there a `null` value in JavaScript?
问题描述
在JavaScript中,有两个值基本上说'我不存在' - undefined
和 null
。
程序员未分配任何内容的属性将是 undefined
,但为了使属性成为可能变为 null
, null
必须明确分配给它。
我曾经认为需要 null
因为 undefined
是一个原始值而且 null
一个对象。它不是,即使 typeof null
将产生'object'
:实际上,两者都是原始值 - 这意味着两者都不是code> undefined 也不能 null
可以从构造函数返回,因为两者都将转换为空对象(必须抛出)在构造函数中声明失败的错误。)
它们在布尔上下文中也都计算为 false
。我能想到的唯一真正的区别是,在数值上下文中,一个评估为 NaN
,另一个评估为 0
。 / p>
那么为什么同时存在 undefined
和 null
只是在试图找出是否已经设置了属性时,会错误地检查 null
的程序员?
我想知道的是,如果有人有一个合理的例子,那么必须使用 null
,这是无法使用 undefined
而不是。
所以普遍的共识似乎是 undefined
意味着'没有这样的property'while null
表示'该属性确实存在,但没有值'。
我可以忍受如果JavaScript实现实际上会强制执行此行为 - 但 undefined
是一个完全有效的原始值,因此可以很容易地将其分配给现有属性以打破t他的合同。因此,如果要确定属性是否存在,则必须使用运算符中的或
hasOwnProperty()
无论如何。再一次: undefined
和 null
的单独值的实际用途是什么?
当我想取消设置不再使用的属性值但我不想 undefined
>删除。我应该使用 null
吗?
问题不是真的为什么JS中存在空值 - 在大多数语言中存在某种类型的空值,并且通常认为它非常有用。
问题是,为什么JS中存在 undefined 值。使用它的主要地方:
- 当你声明'var x;'但是没有赋值给它时,x保持未定义;
- 当你的函数获得的参数少于它声明的参数时;
- 当你访问一个不存在的对象属性时。
'null'肯定也适用于(1)和(2)*。 (3)应该立即抛出异常,事实上它不会,而是返回这个奇怪的未定义,这将在以后失败,是调试难度的重要来源。
*:您也可以认为(2)应该抛出异常,但是您必须为默认/变量参数提供更好,更明确的机制。
然而,JavaScript最初没有例外,或者任何方式询问对象是否有一个特定名称的成员 - 唯一的方法是(有时仍然是)访问该成员并看看你得到了什么。鉴于'null'已经有了一个目的,你可能想要设置一个成员,需要一个不同的带外值。所以我们有'未定义',这是有问题的,如你所指出的,它是另一个伟大的JavaScript'功能'我们永远无法摆脱。
当我想取消设置不再使用的属性值但我不想删除时,我实际上使用了undefined。我应该使用null吗?
是的。保持'undefined'作为信号的特殊值,当其他语言可能抛出异常时。
'null'通常更好,除了在一些IE DOM接口上设置的东西'null'可以给你一个错误。通常在这种情况下,设置为空字符串往往会起作用。
In JavaScript, there are two values which basically say 'I don't exist' - undefined
and null
.
A property to which a programmer has not assigned anything will be undefined
, but in order for a property to become null
, null
must be explicitly assigned to it.
I once thought that there was a need for null
because undefined
is a primitive value and null
an object. It's not, even if typeof null
will yield 'object'
: Actually, both are primitive values - which means neither undefined
nor null
can be returned from a constructor function, as both will be converted to an empty object (one has to throw an error to proclaim failure in constructors).
They also both evaluate to false
in boolean contexts. The only real difference I can think of is that one evaluates to NaN
, the other to 0
in numeric contexts.
So why is there both undefined
and null
if this just confuses programmers who are incorrectly checking for null
when trying to find out whether a property has been set or not?
What I'd like to know is if anyone has a reasonable example where it's necessary to use null
which couldn't be expressed using undefined
instead.
So the general consensus seems to be that undefined
means 'there is no such property' while null
means 'the property does exist, but holds no value'.
I could live with that if JavaScript implementations would actually enforce this behavior - but undefined
is a perfectly valid primitive value, so it can easily be assigned to existing properties to break this contract. Therefore, if you want to make sure if a property exists, you have to use the in
operator or hasOwnProperty()
anyway. So once again: what's the practical use for separate values for undefined
and null
?
I actually use undefined
when I want to unset the values of properties no longer in use but which I don't want to delete
. Should I use null
instead?
The question isn't really "why is there a null value in JS" - there is a null value of some sort in most languages and it is generally considered very useful.
The question is, "why is there an undefined value in JS". Major places where it is used:
- when you declare 'var x;' but don't assign to it, x holds undefined;
- when your function gets fewer arguments than it declares;
- when you access a non-existent object property.
'null' would certainly have worked just as well for (1) and (2)*. (3) should really throw an exception straight away, and the fact that it doesn't, instead returning this weird 'undefined' that will fail later, is a big source of debugging difficulty.
*: you could also argue that (2) should throw an exception, but then you'd have to provide a better, more explicit mechanism for default/variable arguments.
However JavaScript didn't originally have exceptions, or any way to ask an object if it had a member under a certain name - the only way was (and sometimes still is) to access the member and see what you get. Given that 'null' already had a purpose and you might well want to set a member to it, a different out-of-band value was required. So we have 'undefined', it's problematic as you point out, and it's another great JavaScript 'feature' we'll never be able to get rid of.
I actually use undefined when I want to unset the values of properties no longer in use but which I don't want to delete. Should I use null instead?
Yes. Keep 'undefined' as a special value for signalling when other languages might throw an exception instead.
'null' is generally better, except on some IE DOM interfaces where setting something to 'null' can give you an error. Often in this case setting to the empty string tends to work.
这篇关于为什么JavaScript中存在“null”值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!