JavaScript 中 null 是一个对象吗?

发布于 2024-09-17 18:12:55 字数 708 浏览 6 评论 0原文

可能的重复:
javascript 中的空对象

嗨,我已阅读这个线程关于JavaScript中的null,但我现在对null的身份感到非常困惑。

众所周知,由于 语言设计错误,ECMA 指出 null 是空类型

   8.2 The Null Type
   The Null type has exactly one value, called null.

那么为什么人们一直说 null 是一个对象呢?

有人说 null 是单例对象。大家也是这样看待 JavaScript 中的 null 的吗?

Possible Duplicate:
Null object in javascript

Hi, I've read this thread about null in JavaScript, but I'm very confused about the identity of null now.

As it is known that typeof(null) evaluates to object because of the language design error, and ECMA states that null is The Null Type.

   8.2 The Null Type
   The Null type has exactly one value, called null.

So why people keep saying that null is an object?

Someone said null is a singleton object. Is that how everybody sees the null as in JavaScript too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×纯※雪 2024-09-24 18:12:55

Null 是指不存在对象。 Undefined 表示还没有被赋值,null 表示已经被赋值为空。

Null 并不是真正的单例对象,因为取消引用它会导致错误; for (var x in null) 会给你一个错误。回想一下指针时代; null 是指针未指向对象时所具有的值。

Null is the absence of an object. Undefined means it hasn't been assigned yet, and null means it has been assigned to be nothing.

Null is not really a singleton object, because dereferencing it will cause an error; for (var x in null) will give you an error. Think back to the pointer days; null was the value a pointer had when it was not pointing to an object.

落日海湾 2024-09-24 18:12:55

不,null 是少数基本类型之一(其他类型包括数字、字符串、布尔值和未定义类型)。其他一切都是对象,包括函数、数组和正则表达式。数字、字符串和布尔值通常被称为“类对象”,因为它们有方法,但它们是不可变的。 另一方面,对象是可变的。

No, null is one of the few primitive types (others being numbers, strings, booleans, and undefined). Everything else is an object, including functions, arrays and regular expressions. Numbers, strings and booleans are often called "object-like", because they have methods, but they are immutable. Objects on the other hand are mutable.

回心转意 2024-09-24 18:12:55

null 不能被视为对象,因为它没有属性。它是表示原语的关键字,例如 truefalse

> true instanceof Object
false
> null instanceof Object
false

null can't be considered an object because it cannot have properties. It is a keyword representing a primitive, like true and false.

> true instanceof Object
false
> null instanceof Object
false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文