Inconsistency with null

Andrea Giammarchi andrea.giammarchi at gmail.com
Sun May 11 20:56:39 PDT 2014


`null` has been historically ambiguous but because of that, changing its
`typeof` result broke code almost everywhere. Libraries not so quick or
ready to embrace this difference, books talking about `if (typeof obj ===
'object' && obj)` in every bloody example.

However, I always found fascinating the fact that
`Object.getPrototypeOf(Object.prototype)` is indeed `null` ... so if every
object indirectly inherits from `null`, why wouldn't `null` be indeed the
root of all objects and then, due inheritance, being the root of the
`typeof value === 'object'` ?

If you create any other inheritance through a `prototype`, `"[object
ConstructorName]"` is what you get.

Think about the root of all JS objects as `function Null() {}
Null.prototype = Object.create(null)` and consider such value same as
`Empty` is the hidden root of all `Function` instances, including the
`Object` constructor one.

Why would you redefine `null` as variable is a mystery to me, and I love to
believe and trust the fact nobody can.

`undefined` has been the most doomed variable ever, where `var undefined =
true` in the root of any script would have create so many pitfalls in any
code trusting such value that you start thinking `value == null` is the
only way to compare against `undefined` itself, at least in ES3, and also
in every closure based env since `window.undefined` is safe even under
`strict` but as variable can be redefined in any _AMD_ like loader ...

As summary, and in my opinion, `null` is good as it is.

If you want to use `typeof` to deal with it consider the double check,
otherwise consider that `Object.prototype.toString` not only saves you from
`null` mistakes you might have for some reason, it also saves you from `new
Number` and `new String` ... well, it won't save you from `new Boolean`
since knowing it's a boolean but checking without passing through
`valueOf()` won't ever be `false` ... you got the idea, for anything else






On Sun, May 11, 2014 at 8:43 PM, Sakthipriyan Vairamani <
sakthiprofessional at gmail.com> wrote:

> console.log(typeof null);
> ​# object​
>
> ​console.log(Object.prototype.toString.call(null));
> #​
> ​ [object Null]​
>
>
> ​This means that null​
> ​ is expected to be an object. But,
>
>> ​console.log(Object.keys(null));​
>
> ​# TypeError: Object.keys called on non-object​
>
>
>  ​The error message says that null​
> ​ is a non-object. It contradicts the result of typeof and toString.
>
> *Question 1:* Why does it behave so? Is this something which has to be
> fixed?
>
> Also, null is not a keyword <http://es5.github.io/#x7.6.1.1>, but when I
> do
>
>> ​var null = 1;
> # SyntaxError: Unexpected token null​
>
>
> ​It fails with the Syntax Error, since it is a Reserved Word<http://es5.github.io/#x7.6.1>
> .
>
> *Question 2:* What is the purpose of keeping null and boolean literals as
> "Reserved words" and not just keywords?
>
> Note: I tried these samples in node v0.11.13-pre
>
> - Thanks and Regards,
>
> Sakthipriyan Vairamani
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140511/8195e01f/attachment-0001.html>


More information about the es-discuss mailing list