Live data from Hacker News

The void of undefined in JavaScript

shapeshed.com

1–10 of 34 posts

Re: The void of undefined in JavaScript

#3
The correct solution for this problem of undefined is to do nothing!

If someone redefined undefined and it causes a problem - too bad! Some problems are just too stupid to worry about.

Re: The void of undefined in JavaScript

#4
> […] if you throw your scripts out there on the web you've got to expect that somewhere, at some time someone is going to do it […]

And then it will be that persons' problem. Their code would be wrong, not mine. By not making sure my code works when undefined is broken, I would be helping them to realize they have a possible bug in their codebase and that they need to fix it.

Re: The void of undefined in JavaScript

#6

Has anyone ever seen "undefined" be redefined, on one of their professional projects?

A quick search of github gave this line:

   var undefined= undefined;
Which has a comment explaining that it is an optimisation based on the idea that "defined variables are faster than not-defined ones" - I have no idea if that is true or not.

https://github.com/Searle/mothello/blob/c31fc57bedd666e9da34...

I wonder if this counts as redefining undefined though!

The comment also suggests that jQuery does this, which seems to be true as explained in this link:

http://stackoverflow.com/questions/7141106/undefined-variabl...

"undefined in the jQuery code is actually an undefined parameter of a function wrapping the whole code"

Presumably if you have your own local undefined that is guaranteed to be undefined then you are safe from someone else setting it to be something silly.

Edit: The jQuery sources are all wrapped in:

   (function( window, undefined ) {
   ...
   })(window);

Re: The void of undefined in JavaScript

#7

Has anyone ever seen "undefined" be redefined, on one of their professional projects?

It does happen. I worked with one team several years back that had inherited some particularly bad JavaScript code, and this was an issue they ran into several times. They ended up having to audit all of the code for nonsense like this before they could proceed with actual work.

Re: The void of undefined in JavaScript

#8
post #4

> […] if you throw your scripts out there on the web you've got to expect that somewhere, at some time someone is going to do it […] And then it will be that persons' problem. Their code would be wrong, not mine. By not making sure my code works when undefined is broken, I would be helping them to realize they have a possible bug in their codebase and that they need to fix it.

This is one among many serious, and unjustifiable, flaws with JavaScript. It's the kind of issue that should never even arise with anyone's code in the first place, regardless of who wrote the code, because the language and its implementations should not allow it to happen.

And, yes, we know that other languages have flaws, too. But aside from perhaps PHP, the flaws in other languages are almost never as outright stupid as they are with JavaScript.

Re: The void of undefined in JavaScript

#9
post #6

Has anyone ever seen "undefined" be redefined, on one of their professional projects?

A quick search of github gave this line: var undefined= undefined; Which has a comment explaining that it is an optimisation based on the idea that "defined variables are faster than not-defined ones" - I have no idea if that is true or not. https://github.com/Searle/mothello/blob/c31fc57bedd666e9da34... I wonder if this counts as redefining undefined though! The comment also suggests that jQuery does this, which see…

If you have to guard against someone doing something silly, then you are never safe. The problem is not the language at that point, it's the environment.

Re: The void of undefined in JavaScript

#10
Isn't this whole issue just a minor version of the problem Ruby has with monkeypatching? The fact that a Ruby guy can define `method_missing` to allow for bare strings shows how Ruby is cool (though you should never do that), but the fact that you can redefine `undefined` in JS shows how JS is stupid (despite the fact that you should never do that). I don't understand the dichotomy.
Post reply on HN