为什么 JavaScript 中有一个 `null` 值? [英] Why is there a `null` value in JavaScript?

查看:22
本文介绍了为什么 JavaScript 中有一个 `null` 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JavaScript 中,有两个值基本上表示我不存在" - undefinednull.

In JavaScript, there are two values which basically say 'I don't exist' - undefined and null.

程序员未分配任何内容的属性将是undefined,但为了使属性成为nullnull 必须明确分配给它.

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.

我曾经认为需要 null 因为 undefined 是一个原始值而 null 是一个对象.不是,即使 typeof null 会产生 'object':实际上,两者都是原始值 - 这意味着既不是 undefined 也不是 null 可以从构造函数返回,因为两者都将被转换为空对象(必须抛出错误才能在构造函数中声明失败).

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).

它们在布尔上下文中也都评估为 false.我能想到的唯一真正区别是,在数字上下文中,一个计算结果为 NaN,另一个计算结果为 0.

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.

那么为什么同时存在 undefinednull属性是否已设置?

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?

我想知道的是,是否有人有一个合理的例子,说明必须使用 null 而不能用 undefined 代替.

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.

因此,普遍的共识似乎是 undefined 表示没有这样的属性",而 null 表示该属性确实存在,但没有价值".

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'.

如果 JavaScript 实现实际上会强制执行此行为,我可以接受这一点 - 但 undefined 是一个完全有效的原始值,因此可以轻松地将其分配给现有属性以破坏此合同.因此,如果您想确定某个属性是否存在,则无论如何都必须使用 in 运算符或 hasOwnProperty().再说一次:undefinednull 的单独值的实际用途是什么?

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?

当我想取消设置不再使用但不想删除的属性值时,我实际上使用了undefined.我应该使用 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?

推荐答案

问题并不是真正的为什么 JS 中存在空值"——在大多数语言中都有某种空值,并且通常被认为是非常有用.

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.

问题是,为什么 JS 中有 undefined 值".主要使用场所:

The question is, "why is there an undefined value in JS". Major places where it is used:

  1. 当你声明 var x; 但没有赋值给它时,x 保持 undefined;
  2. 当你的函数得到的参数比它声明的少时;
  3. 当您访问不存在的对象属性时.
  1. when you declare var x; but don't assign to it, x holds undefined;
  2. when your function gets fewer arguments than it declares;
  3. when you access a non-existent object property.

null 当然对于 (1) 和 (2)* 也同样有效.(3) 真的应该立即抛出异常,事实上它不会返回这个奇怪的 undefined 而稍后会失败,这是调试困难的一个重要来源.

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 of returning this weird undefined that will fail later, is a big source of debugging difficulty.

*:您也可以争辩说 (2) 应该抛出异常,但是您必须为默认/变量参数提供更好、更明确的机制.

*: 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.

然而,JavaScript 最初没有例外,也没有任何方式询问对象是否有某个名称的成员 - 唯一的方法是(有时仍然是)访问该成员并查看您得到什么.鉴于 null 已经有一个目的,并且您很可能希望为其设置一个成员,因此需要一个不同的带外值.所以我们有 undefined,正如你所指出的,它是有问题的,这是我们永远无法摆脱的另一个伟大的 JavaScript '特性'.

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.

当我想取消设置不再使用但我不想删除的属性的值时,我实际上使用了 undefined.我应该改用 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?

是的.保持 undefined 作为特殊值,以便在其他语言可能会抛出异常时发出信号.

Yes. Keep undefined as a special value for signaling when other languages might throw an exception instead.

null 通常更好,除了在某些 IE DOM 界面上,将某些内容设置为 null 可能会给您一个错误.通常在这种情况下设置为空字符串往往会起作用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆