JavaScript 中 null 是一个对象吗?
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.不,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.null
不能被视为对象,因为它没有属性。它是表示原语的关键字,例如true
和false
。null
can't be considered an object because it cannot have properties. It is a keyword representing a primitive, liketrue
andfalse
.