I think that undefined
(or window.undefined
) is a constant variable, not a reserved word (like NaN
, Infinity
, unlike null
).
When use UglifyJS to compress a Javascript file which use undefined
frequently, it is good if declare a local variable to hold undefined
.
For example:
function main() {
var undefined;
...
}
UglifyJS will gives me:
function main(){var n;...}
EDIT
Thanks @T.J. Crowder! Now I have my own choice. I'm sure that undefined
, NaN
, Infinity
, and window
is not a reserved word, they just "read-only". So, there're no problem to declare a local undefined
(even if strict mode). I also don't worry about the maintain issues of confusion, I just need to write a // comment
or a /* comment */
to explain what does that means.
Whether it's "good" is a matter of opinion, so let's leave that off to the side.
Positives:
undefined
s become n
or similar.Negatives:
There's no technical reason for not doing it (other than maintainability). In your scenario, the variable will have the genuine value undefined
. In fact, it used to be common to write general purpose libraries like this:
(function(undefined) {
// ...
})();
...which is just a variation of what you're proposing. Why did the authors do that? In case some eejit did this outside their code:
undefined = 42;
The pattern above ensures that the undefined
identifier within the scoping function really has the value undefined
(since we don't pass any arg when calling the scoping function).
That's not an endorsement, just highlighting that there isn't a technical issue with doing it.
You don't see that pattern so much anymore, because the 5th edition specification (2009) made the undefined
global variable (and several others) readonly. Assigning to it stopped changing its value. See Annex E and section §15.1.1, which it references:
15.1.1: The value properties NaN, Infinity, and undefined of the Global Object have been changed to be read-only properties.