typeof null, and criticism of idiots who wrote isNull

Garrett Smith dhtmlkitchen at gmail.com
Sun Aug 17 01:10:54 PDT 2008


Something I read on the wiki.

http://wiki.ecmascript.org/doku.php?id=proposals:typeof

Douglas Crockford:
| I think it is too late to fix typeof. The change proposed for
| typeof null will break existing code. For example, isNull
| functions have been written by idiots as
|
| function isNull(a) {
|  return typeof a == 'object' && !a;
| }
|
| It is possible to write an isNull that performs correctly with
| the fixed typeof, but existing code cannot anticipate the change.
|
| Perhaps a better alternative is a new operator, maybe called
| typeString, which works the way typeof should work, including
|
|  typeString [] === 'array'
|
| (Hi Doug – please sign your comments. Use the feather-icon button at
| the right end of the in-page toolbar. Thanks!)
`---------------------------------------------------------------

It does seem poor choice, as the anonymous post indicates, to use an
isNull function. x === null, inline, is clearer, is much faster, and
doesn't introduce an unneeded dependency. It seems that the post
calling the authors of this code "idiots" is apparently the author
himself. Yeah? Maybe it's a joke?

http://groups.google.com/group/comp.lang.javascript/msg/30ea9f75ccc87524?dmode=source

Douglas Crockford:
| Yup. I put stuff like that into so functions. For example,
|
| function isFunction(a) {
|    return typeof a == 'function';
| }
| function isNull(a) {
|    return typeof a == 'object' && !a;
| }
`---------------------------------------------------------------

Oh well. Most of us write dumb stuff once in a while. I just thought
it was odd: Saying such code "has been written by idiots" was
apparently written by the very same person.

I can't find real world code using such isNull code -- that
c.l.javascript post is the only place I found it (though I didn't
search that hard).


Garrett


More information about the Es-discuss mailing list