-2

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.

DMaster
  • 603
  • 1
  • 10
  • 23
  • This is a matter of opinion, and so it's off-topic for SO. – T.J. Crowder Sep 11 '15 at 13:20
  • By "good" you mean, because it results in a smaller compressed file? – T.J. Crowder Sep 11 '15 at 13:21
  • No, it isn't good. Regardless of whether it works or not, it's always good to avoid any words reserved by the language. To paint a better picture - you can walk through mine field and you can avoid stepping on the mines and you can get to the other side. Or, you can use another field which has no mines and still get to the other side of the field. Result = the same. Risk with mine field = huge. Risk with regular field = none. Easy choice. No need to look down the barrel of a loaded gun, is there? :) – Mjh Sep 11 '15 at 13:21
  • If you're concerned about the length of `undefined` you could use `void 0` in its place. But the solution you have come up with is somewhat commonly seen, especially in the form `(function(a) { /*a is undefined*/ }());` – James Allardice Sep 11 '15 at 13:22
  • 3
    I guess it should be asked [at Code review](http://codereview.stackexchange.com/) – MatthewRock Sep 11 '15 at 13:36
  • 1
    *"I'm sure that undefined, NaN, Infinity, and window is not a reserved word"* That's correct. [Here's the list of reserved words](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-reserved-words), [here's where the now-read-only globals `Infinity`, `NaN`, and `undefined` are defined](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-value-properties-of-the-global-object). `window` is only defined on browsers, and is also a variable, not a keyword. – T.J. Crowder Sep 11 '15 at 13:49
  • @T.J.Crowder That's also correct in *ECMAScript 5*, not only in *ECMAScript 6*. Plus, I shall never support Javascript version earlier than ECMAScript 5. – DMaster Sep 11 '15 at 14:09
  • @DMaster: Right -- I was just linking to the current spec. The change was in ES5 in 2009, as I mention in my answer. – T.J. Crowder Sep 11 '15 at 14:11

1 Answers1

2

Whether it's "good" is a matter of opinion, so let's leave that off to the side.

Positives:

  • Smaller compressed file, because all your undefineds become n or similar.

Negatives:

  • Maintenance issues around the confusion it may cause.

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Wouldn't it have faster lookups on undefined? I guess it wouldn't make that much of a difference. – MinusFour Sep 11 '15 at 13:30
  • 1
    @FelixKling: No, *I* am, thanks. I have this blind spot where I throw `undefined` in with `arguments` and `eval`, which you can't use as identifiers in strict mode. (This is at least the third time.) But they left `undefined` off that list -- possibly because of the pattern I mention above, to ease transition to strict mode. – T.J. Crowder Sep 11 '15 at 13:40
  • @MinusFour: In principal it would. In practice, since the global is now read-only, if you don't shadow it in any intervening scope, the engine may optimize away that traversal anyway. – T.J. Crowder Sep 11 '15 at 13:46