In John Resig's slideshow on how he was building jQuery 1.4, he mentioned a point where he added an undefined
variable to the jQuery closure because "we can re-use (the variable)".
undefined
is not an ordinary variable:
> var undefined = 4
undefined
> undefined
undefined
Therefore, we know that undefined is not a variable. So why would an undefined
be re-undefined in the jQuery source?
Because in some JavaScript engines it's possible to set undefined
to a value. This is to make sure undefined
is really undefined
.
Additionally to +Rocket Hazmat's answer, you can reduce the file size after compression a bit, when your code uses undefined
frequently. That's because a local variable undefined
may have its name mangled by the compressor, while the global undefined
may not:
foo === undefined;
// ^----- don't touch this, put "undefined" in the compressed result
(function (undefined) {
foo === undefined;
})();
// may however be mangled to
(function(u){foo===u})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With