The void of undefined in JavaScript
shapeshed.com
shapeshed.com
If someone redefined undefined and it causes a problem - too bad! Some problems are just too stupid to worry about.
That is incorrect. It might be correct if the word 'library' was replaced by 'code snippet' to indicate a copy-and-paste-of-your-code issue. But if you've created a library then your functions are over in some other file, where `typeof undefined === "undefined"`.
The proper attitude here is the same as the Python attitude towards not having `private` attributes: "If some other programmers want to do something crazy with my code, that's their prerogative. If it blows up in their faces, that's their problem."
If `undefined` is an argument name, it's only bound inside that function and nowhere else.
The Crockford quote (characteristically dogmatic, to the effect of 'void means something different in JS than in Java, so AVOID VOID!') is also a kicker.
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.
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.
Citation needed. There are tons of languages with huge fucking flaws to blow your code and kick your dog.
PHP and Javascript are relatively harmless (if a little brain damaged). At least you don't get buffer overflows using them.
You think C++ has a better design, for example? People forgot how bad Python used to be, pre 2.4?
Nope, as much as I like to rail against the sins of C++, it is a paragon of design virtue next to JS. I've been programming for three decades now and JS is the worst language I have ever seen. I use JS a lot in my day job and it has parts I really like (object constants, for example) but really, as a piece of language design it is really the pits.
If you're writing new C++ code, you're in no way forced to use it. You can use constants or inline functions to achieve the same result in almost all cases.
This really boils down to "my code won't act the same way if I give unfettered access to my environment to unvetted code" which is true in almost any situation. If you're having problems because "undefined" is being redefined, you have bigger and more fundamental problems. There are a lot of bad things about JS, but this is not one of them - its overly nitpicky and completely unfair. This same capability (of being able to redefine almost anything) is lauded as part of Ruby, but when it could theoretically cause any easy-to-avoid problem in JS, it's just more proof that JS is a shitty language.
Sorry if I'm coming across as combative - I don't mean to. I just think this whole snide criticism of JS for every little thing is silly and unhelpful, and has more to do with a superiority complex than actual technical issues.
In Ruby, nil, is a keyword so you can't assign to it nor can you use it as a method parameter.
This seems like a solution in search of a problem.
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);One of the nice things about JavaScript is that it gives you the ability to accomplish things many different ways, but if someone uses that to shoot themselves in the foot then that is something they need to correct.
(function( window, undefined ) {
// ...
})( window );I guess my any languages are different. Never realized it's importain in haskell (except it is ⊥).