Hacker News new | past | comments | ask | show | jobs | submit login

I can't believe no one has posted the (in)famous WAT ruby/javascript presentation: https://www.destroyallsoftware.com/talks/wat

It describes many ridiculous things in javascript such as empty array + empty array == empty string, etc. It is also great for a good laugh to anyone who has ever written javascript or ruby.




Many of those examples also make sense when you consider what you're actually asking for.

For example, Arrays have no + operator. So []+[] requires that [] be converted to a string first. The string representation of [] is "" (although some interpreters use "[]" or "[Array]" for clarity), so []+[]==""+""=="".

Similarly, he used the example []+{}, and claimed it was [object Object]. This actually wrong (at least in Chrome); it's actually "[object Object]", a string. The reason for this is the same as the previous example.

However, there is a different reason why {}+[]==0, given that "[object Object]"+[] == "[object Object]". There turns out to be something special about {} at the beginning of a statement (It's a block, or something). Thus, {}+[] is like {};+[], where +[] causes [] to be converted to a number: 0. Compare: {}=={} (syntax error, unexpected token ==). Yes, this one is actually confusing. (However, expressions are not usually evaluated in void context.)

He also observes that ",,,,,,,,,,,,,,," is a bad representation of Array(16). It is, since the undefined in each element is important, but again, that can be solved by using an interpreter with better string generation. (Or a version of IE that doesn't attempt to be clever and just returns "[Object]" all the time).

So perhaps the moral of the story is: Yes, it's an entertaining talk, but a several of the examples break down because his interpreter is vague.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: