by neilk · 15y ago · 0 comments
Of course due to a bug in the old (v3) spec, you could redefine the builtin undefined as well as some other builtins like NaN and Infinity, but you shouldn't really be doing that and the spec at least is now fixed.
In the meanwhile, to fight that, this is a commonly used pattern for browsers: (function(window,undefined) {
// code //
})(this);
Unfortunately, I recently profiled my code and found that those functions were being called an absurd number of times during a moderately complicated page load, so they're gone now. :-(
I just tried timing it in modern Chrome. The overhead of calling a function like isDefined(), versus typeof x !== 'undefined', seems trivial. On my machine, after 100,000 calls there's a difference of 50ms.
Still, I concede that there are some situations where it would matter, if you were driving an animation. And in older browsers it might matter a lot even in other circumstances.