5 ms·
I often use the improved typeof function: http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ http://javascriptweblog.wordpr
by firefoxman1 14y ago
I often use the improved typeof function: http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-...
Question: why is the way to determine null considered "very bad"? It works and seems like a valid way, since both empty objects and arrays are truthy, null is the only falsy "object" type.
- sjs 14y agoI think it's only bad because it would break with future changes to typeof. Of course I may be completely wrong on that. This works just as well and is more future proof though: function isNull(x) { return x === null } At which point you don't really need a function at all. Perhaps the version using typeof is from a time when there was no === operator. Was that pre-ES3? I think so but am not certain.
- firefoxman1 14y agoOh yeah, of course. There's no need when you can just do === null.
- krisk 14y ago"I think it's only bad because it would break with future changes to typeof" You're completely right. The proposal (http://wiki.ecmascript.org/doku.php?id=proposals:typeof http://wiki.ecmascript.org/doku.php?id=proposals:typeof) suggests the change of typeof null to actually return "null"; however, as Douglas Crockford points out, "existing code cannot anticipate the change". Additionally, Brendan Eich states that "neither this proposal nor the bug fixes in that proposal are backward-compatible".