As much as typeof null === "object" makes sense because it represents "no object", it may have been a bug. Even Brendan Eich, the creator of JavaScript, thinks so (skip to the bottom of the page). To quote: "We have reason to believe typeof null === “object” is a bug that could bite real content, from our spidering of the web."
But of course, it's not going to be fixed because it'll break too much existing code, so it's one we have to live with.
It does seem like a stereotypical "JavaScript developer" like mistake. And it isn't even JavaScript specific. I'm actually kinda impressed, In a "I need a drink" kind of way.
iirc, it's actually defined by the HTML spec… which specifically notes that it requires behaviour (in terms of the extent it pretends not to be defined) not provided for by anything in the ECMAScript spec.
Edit: also, something that wasn't mentioned above: IE exported elements by their IDs as attributes on both document.all and the global object (i.e. window), so it's not merely a list. Whereas Netscape 4 and down exported them as attributes on ancestor nodes, including document. Netscape 6 and up dropped this altogether in favour of the W3C model though.
iirc, it's actually defined by the HTML spec… which specifically notes that it requires behaviour (in terms of the extent it pretends not to be defined) not provided for by anything in the ECMAScript spec.
That was the case until a year or two ago. The behaviour of document.all used to be defined in the WhatWG spec as a willful violation of the ECMA-262 spec, but as of ES2018, there's a new internal slot defined in Annex B called IsHTMLDDA that describes its behaviour, so it is no longer a willful violation.
View all comments
Show parent comment
130
u/slakkenhuisdeur Jan 01 '20
This is probably THE most JavaScript thing I learned to date.