Javascript’s null Value Has an Identity Crisis
Monday, June 12th, 2006I came across another identity crisis in Javascript while working on my $class library. This time, it is the null value that needs help. Observe the output of the following expressions:
This time, all browsers agree: null is an “object”, but not an instance of Object. What does it mean? The way I see it, you can not use null as an Object, so it should have no relation to Object at all. You cannot access or create properties on null, you cannot evaluate null.toString(), etc.
My solution in the $class library library is similar to what I’ve done for undefined. I created a dummy class called Null and put a special case in the $class.instanceOf and $class.getClass methods to consider the null value to be an instance of Null and nothing else.