Live data from Hacker News

The void of undefined in JavaScript

shapeshed.com

11–20 of 34 posts

Re: The void of undefined in JavaScript

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

People don't typically inject frequently changing, unvetted advertising code into their ruby runtimes. Generally, the most frequently that the ruby code in your runtime changes is each deploy.

Re: The void of undefined in JavaScript

#12
There are a lot of potential pitfalls in coding JavaScript. Redefining undefined is only one of many.

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.

Re: The void of undefined in JavaScript

#13
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.

indeed ... I'm surprised no one has mentioned "wat" yet :P https://www.destroyallsoftware.com/talks/wat

Re: The void of undefined in JavaScript

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

People don't typically inject frequently changing, unvetted advertising code into their ruby runtimes. Generally, the most frequently that the ruby code in your runtime changes is each deploy.

Granted, but that's an incidental problem, not one arising from JS being a bad language. Also, I highly doubt there is much advertising code that changes the value of "undefined" - certainly not any I've encountered.

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.

Re: The void of undefined in JavaScript

#15
I tend to read these sorts of articles as "don't do stupid things". Who in their right mind would name an argument undefined? (or use a library by someone who would do so) The operative sentence in the article to me is "can be avoid if you understand how it works". On an unrelated note I'm not sure if that's a typo or a pun.

Re: The void of undefined in JavaScript

#16
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.

indeed ... I'm surprised no one has mentioned "wat" yet :P https://www.destroyallsoftware.com/talks/wat

Absolutely brilliant/hilarious video.

Re: The void of undefined in JavaScript

#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 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."

Re: The void of undefined in JavaScript

#19
post #14

Earlier quoted context omitted.

People don't typically inject frequently changing, unvetted advertising code into their ruby runtimes. Generally, the most frequently that the ruby code in your runtime changes is each deploy.

Granted, but that's an incidental problem, not one arising from JS being a bad language. Also, I highly doubt there is much advertising code that changes the value of "undefined" - certainly not any I've encountered. 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 "undefin…

In general you are correct, redefinition in Ruby can be abused, but it isn't so easy to abuse 'nil' in Ruby as it is to abuse 'undefined' in Javascript.

In Ruby, nil, is a keyword so you can't assign to it nor can you use it as a method parameter.

Post reply on HN