Is null an object?
Possible Duplicate:
Null object in javascript
Null is an object right? So if i set x to null, why can't i get the constructor value?
var x = null;
alert(typeof x);
alert(x.constructor);
null in JS is a primitive value. It wasn't constructed by any constructor function, so it doesn't have a constructor property. typeof null being 'object' is basically a horrible lie retained for historical reasons. Don't expect too much consistency from JS, you'll be disappointed!
Primitive values can often behave like objects in JS due to autoboxing: (1).constructor works despite 1 also being a primitive value, because it implicitly calls new Number(1) . But there is no Object form of null (or undefined ) to automatically convert to, so in this case you get an error.
In general you should avoid using constructor . For many class/instance models built on top of JS it doesn't do what you think at all. instanceof typically works better.
Null不是Javascript中的对象 - null是null基元类型的单数实例,因此没有构造函数。
上一篇: null对象是JavaScript中的对象,为什么它不被修复?
下一篇: 对象是否为null?
