Live data from Hacker News

The void of undefined in JavaScript

shapeshed.com

21–30 of 34 posts

Re: The void of undefined in JavaScript

#21
post #18

> Let's say someone is using your library within that function and you reference undefined. You get the string "oops". Oops indeed. 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…

I used to believe this, but I now realise it's insufficient when building large apps. Upgrading the version of Django (or indeed Rails) you depend on requires a comprehensive testsuite and involves changes to your code. Upgrading the version of Wicket you depend on is easy because the framework is able to enforce that you only access the attributes you're meant to, only subclass things that are designed to be extended, etc.

Re: The void of undefined in JavaScript

#22
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…

It also allows minifiers to minify that "undefined" variable. Not that i think it's good practice to use those kind of hacks that gzip compression makes obsolete :P

Re: The void of undefined in JavaScript

#23
post #18

> Let's say someone is using your library within that function and you reference undefined. You get the string "oops". Oops indeed. 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…

Even if it was in the same file, as long as it's in a different function it'll work.

If `undefined` is an argument name, it's only bound inside that function and nowhere else.

Re: The void of undefined in JavaScript

#24
I find the cult of 'oh my god you can redefine undefined' hilarious. Yes, you can. You can also redefine Array, Object, and so on. If you're writing a script to intentionally disrupt a system, for(;;); will also do. This doesn't happen in sane environments, so it really isn't a problem.

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.

Re: The void of undefined in JavaScript

#25
> If you have done more than one day's programming in any language you will realise that this is an important building block for programmers.

I guess my any languages are different. Never realized it's importain in haskell (except it is ⊥).

Re: The void of undefined in JavaScript

#27
post #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.

Exactly. I'm tired of these alarmist articles with "you must do this" solutions to non-existent problems. I always think "oh, shi-" before I realize the problem affects exactly no-one.

Re: The void of undefined in JavaScript

#29
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 stu…

>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?

Re: The void of undefined in JavaScript

#30
post #25

> If you have done more than one day's programming in any language you will realise that this is an important building block for programmers. I guess my any languages are different. Never realized it's importain in haskell (except it is ⊥).

Exactly. There is no void in hardware, for example. Also, there's no bottom either.
Post reply on HN