Why reuse of `undefined`?

In John Rezig 's slideshow on how he builds jQuery 1.4, he mentioned that he added the undefined variable to the jQuery closure because "we can reuse (the variable)".

undefined not a regular variable:

 > var undefined = 4 undefined > undefined undefined 

Therefore, we know that undefined is not a variable. So why would undefined be re undefined in jQuery source?

Slide 31

+7
javascript jquery
source share
2 answers

Because on some JavaScript machines, you can set the value to undefined . This means that undefined really undefined .

+10
source share

In addition to the + Rocket Hazmat answer, you can reduce the file size after compression a bit when your code often uses undefined . This is because the local undefined variable may have its name distorted by the compressor, while the global undefined may not be:

 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})(); 
+5
source share

All Articles