Is null an object in JavaScript? [duplicate]
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?
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.
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
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
精彩评论