This appears to be a peculiarity of the REPL implementations, not a difference between V8 and JSC. d8 (and the Chrome console) behaves the same way as jsc, while you can make d8 and jsc behave the same as the node REPL by wrapping each statement in parentheses:
No, it's more subtle than that. By wrapping in parenthesis, you're making it an expression. When you just say "{}+[]" in e.g. Chrome, the first {} is parsed as a block. So what you see printed is the result of "+[]", which is 0. This is why {}+[] is not equal to {}+[] without parens. This may also be why Node gives different results; I'm not sure.
I didn't mention any of this in the talk (it would've killed the flow ;). Instead, I glossed over it and interpreted the syntax as any sane programmer would.
I understand why it happens. I'm saying that what you're pointing out is not in the nature of JavaScript, it's in the REPL. It's just wrong; there is no other situation where that code would evaluate that way. node is behaving "properly", showing you what value you can expect if you, say, assign that expression to a variable.
I guess it's an amusing bit of sleight-of-hand, but using it to mock JavaScript seems, I don't know, tasteless. Doesn't it have enough problems without inventing more?
To clarify, I thought the first part of the talk was funny. Here's a weird bit of Ruby. Hah! Here's a weird bit of JavaScript. Hah! Here's some JavaScript to make you think it's weird in a way it actually isn't! Uh, okay? Just don't see what's funny about that, I guess.
The other reply is right, to clarify String({}) === "[object Object]"
I think the actual video was a little misleading, [] + {} == the string "[object Object]" not an object. The square brackets are just part of the tostring method and are unrelated to the square brackets of arrays.
But of course, anyone that actually wants to protect against XSS attacks won't allow user input to be evaluated. If they did want to allow user-supplied Javascript, they wouldn't blacklist, they would whitelist (by parsing the user-supplied script and using the AST to emit only whitelisted operations).
This is incredibly funny, but does anyone know why the interpreter makes such bizarre decisions? Why wouldn't it be preferable to just throw up errors?
JavaScript has a strong design principle of not throwing errors for syntactically valid constructs. The most common way it accomplishes this is through gratuitous use of implicit type conversions. It does a lot of implicit string conversions, even on things that you would think of as error codes, like undefined or NaN.
JavaScript went down the path of being a "forgiving" language because it was intended to provide "extra" (non-core) functionality, under a lot of environments, in the hands of non-experts. It made sense at the time that it should fail silently rather than noisily to not crash the page it was on. It also tried to help amateurs who just banged on code until it did what they wanted, by generally doing something rather than nothing, and by trying to infer intended behavior from undisciplined code. It was never intended to make large-scale or robust applications, so it didn't make decisions that would facilitate that use-case.
I wish there was some technology that could take a bunch of code and warn you of common errors before having it execute and 'noisily crash the page it was on'.
The Ruby stuff apparently isn't bizarre. From what I've read, Ruby creates variables as it encounters an assignment to them when parsing the code. So, when you have
a = a
in your code, Ruby creates the variable a before it ever tries to actually execute the assignment. If later, when it has finished parsing everything, and starts executing, that statement fails because b is not defined, you still end up with variable a being defined, and since it has not had anything successfully assigned to it, it has a value of nil.
The JavaScript stuff, I think, comes from operator overloading. The plus operator is overloaded to allow adding strings to concatenate them, and it will do type conversion to get compatible types, so "wat"+1 results in the 1 being converted to a string, and then the strings are concatenated. Since the minus operator is not so overloaded, "wat"-1 instead is treated as numerical subtraction. JavaScript allows string to be used as numbers, so "123"-1 gives 122. However "wat" is not a string that represents a number, so gives NaN when forced to be treated as a number, and "wat"-1 is thus NaN.
Javascript was a rush job. And the ECMA spec was needed early due to industry pressure. So where perl and (to a lesser extent) python were able to just bury their early goofs in a pile of incompatible versions bumps, Javascript had its encased in the proverbial carbonite of an international standard. Which is sad, really, because in a lot of other ways it's the cleanest scripting language of its generation.
Guess I'm not a hacker? It's like watching a rerun I've seen twice over with the laugh track turned up to 11 (just to clarify, someone said it was a repost but I haven't seen it before).
Really? You don't think this is funny at all? Maybe it's because I'm in my mid twenties, but I think my father would even find this funny and he knows little to none about programming.
The humor is playing on an inclination to laugh at things we don't understand, which seems counter to the hacker ethos. Software is hard, and there may be deep reasons in the dynamic type systems that cause these languages to work like this, or there may not be, but either way this type of thing is interesting and seems to demand some further explanation and/or justification. I find it interesting but don't get the humor. Perhaps, as you implied, I'm just too old.
If there were jokes formed around the nonsensical behavior of these languages, then it might be funny. But I don't find it very entertaining to just point them out.
"Wat" at the start really turned me off. I realize that was probably (?) part of the shtick, but it's one misspelling in particular that really drives me nuts. Real hacker humor is fairly dry (the story about magic comes to mind); this felt more like script kiddie-level humor. I can see valid points (and humor) raised with the Ruby interpreter, but the delivery could have been better--at least more adult-ish.
I managed about halfway through. I did have the audio turned off, so I'm assuming I missed out on some of the redeeming qualities it may have had. Given the liberal usage of "wat" throughout what I did watch, I suspect that my gut feel is likely correct.
Right. It was marginally informative, the meme pics were chuckle-in-my-head worthy but what really got me laughing was that it's on the front page of HN. Its so sad you have to laugh to keep from crying.
"Pompous?" Perhaps, but I think you'll agree that there are far more redeeming and clever ways to construct a humorous commentary on the behavior of (for example) an interpreter.
To address your particular concern which I feel is somewhat misplaced: I think it would be more pertinent to classify individuals like myself and the others who have complained about the delivery of this particular piece as suffering from less patience, especially with the usage of "wat" throughout. There are reasons for this, and it likely depends on an individual's preferences, how one feels about the (mis)usage of language, or the (ab)use of memes to attempt the creation of a witty informational exchange. Note: I realize that I'm probably just slightly outside the target demographic of this video, which probably appeals more to individuals who are in their mid-twenties or younger, and I'm fully aware of this fact.
I apologize if I personally came off as a pompous whiner. This particular misspelling of "what" is a personal pet peeve that drives me absolutely insane, and it greatly reduces my mental perception of an item's quality (or the quality of an individual's speech). I suppose other complaints probably derive from its delivery as a meme-inspired video, which probably appeals to younger audiences as I've mentioned.
In other words: There are legitimate gripes about this form of video. I've seen similar ones that were indeed humorous, but they had a fantastic delivery that was well thought-out. Including a dozen absurd pictures that appear to be sourced from sites like Imgur with "WAT" emblazoned on them does not appeal to everyone, and I don't think it's useful to criticize others' opinions of this video as sorely lacking in humor as "sad" simply because we all have greatly different tastes and preferences. Moreover, these tastes and preferences are malleable; something you found hilarious when you were 22 might be head-shakingly awful by the time you're in your 30s.
I suspect this video is one of these. Save it and watch it again in ten or fifteen years.
I suppose I came off as snarky with my comment but I still don't think this was HM worthy. My reason for disliking it was because it simply showed a bunch of examples of obscure flaws in languages that aren't really useful. There wasn't enough substance to it. It was just "look at the weird result you get when you write code in a way that you never would in the real world. Wat?". Meme pics are funny. Meme pics and a good talk are funny. This was meme pics and a really useless, trying too hard talk. I've seen presentations that offer up a lot of value in terms of stuff you can actually use and topics that are really relevant. This was just language quirks and nothing more. No talk about why these quirks exist, uses for them, how they could affect you without knowing, none of that. It was just bad humor and no substance. I'm not against humor showing up on HN from time to time but this was more like something that belongs on Reddit or even any random forum. Oh, and the title too bothered me. It was kind of immature.
And for the record, I'm 25 so either I'm really mature or I have no sense of humor. The jury is out. I'm definitely still not hacker though as I watched it again and still barely chuckled.
Eh, I'm sure my original comment was somewhat snarky, too. I simply cannot shake the feeling that you were downvoted because there's a large number of people who find the meme overuse outright hilarious (I don't; if I want memes, I'll visit Imgur's gallery--they can be funny, but I found their use in the video, well, useless). It's a shame, too, because I felt that there was a fairly strong sentiment in agreement with you (including myself, so I'll admit bias).
Regardless of what others might feel, you're absolutely right. The title had about as much redeeming value as the video. :)
I'll join you in the non-hacker, no-humor camp. This is particularly true since my inner grammar Nazi finds the abuse of "what" to be abhorrent...
I didn't laugh until the end. Partially, I'm sure, because I didn't want to lie to myself by intentionally laughing just to feel like a hacker. But when I saw NaNNaNNaNNaN...(etc) coming, I couldn't help it.
At least Arch has `js`package which gives you js command to obtain JS shell; in fact it is just Mozilla's SpiderMonkey. Anyway, the best option for "standalone JS" is node, mainly because it has a sane way of importing code from other files.
My Chrome install on Windows has the same thing. I push ctrl+shift+c to load the developer tools, and click on console and it's a very similar tool to what he was using.
<shamelessplug>If you want to use any of these inside emacs check out my js-comint mode: http://js-comint-el.sourceforge.net/ Makes it really easy to do things like write code in one buffer and execute it in another. </shamelessplug>
No. I'm with you man. The way he said it was seriously bugging me. At the same time I couldn't justify my annoyance with an alternative pronunciation that made sense.
It's Keynote. I recorded those sessions as screencasts, then sliced them into tiny pieces so I can advance them perfectly with my talking. There are 37 slides in that four-minute talk. In some cases, the slices are only two or three frames long.
I like the sliced screencasts because they're so repeatable. I practice my talks a lot, and I can get the timing down perfectly because it's always the same. Highly recommended. :)
Well, .join(x) joins an array into a string, with x as the separator. First of all what does Array(16) make? Effectively it should make an array with null 16 times. Now, JavaScript seems to think null is a blank string, a string with the text 'null', or a zero depending on the context.
The first thing it should do is TypeError that null is not a string. Or don't join null fields because there is nothing there.
If is does convert null to '', then it should TypeError on string - int. If it is not going to TypeError it should do something sensible, like slice the string
I think the correct thing would be to TypeError in two different places. Or change the language so things that don't error actually work. Returning a string with NaNs in it will just lead to uncaught errors. If it actually returns something it should probably be ' Batman!'.
JavaScript nuttiness:
Now it's zero:
> null + 1
1
Now it's "null":
> null + "text"
'nulltext'
Now it's a blank string:
> [null, null, null].join()
',,'
Python errors when things don't make sense, and forces you to make them make sense:
If it doesn't work, error:
>>> "wat" - 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'int'
Can't join None into a string:
>>> ','.join([None, None, None])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, NoneType found
Convert None to a string if you want:
>>> ','.join([str(i) for i in [None, None, None]])
'None,None,None'
But it makes more sense to drop the None:
>>> ','.join([str(i) for i in [None, None, None] if i])
''
That's probably one of the more logical examples. I'd have expected an exception from any other language, though (Javascript I know doesn't tend to type error).
I guess you had to be there. I can see why someone might find this funny, but honestly, nothing is that funny. Some behavior in Javascript is surprising, but not that surprising.
If you've ever gone through a phase (or a graduate degree!) of thinking deeply about programming language design, you might, as I do, find Javascript's expression evaluation behavior to be just about the funniest thing around.
I have but I don't. Javascript was designed to reduce programmer-visible errors at the cost of making it nearly impossible to write a correct program. At that goal, it succeeds, and [] + {} being NaN makes perfect sense under those constraints.
It's on the front page, but at the bottom. DAS is a small one-man business, so I did the design myself (and I'm not a good designer). Sorry about that. :)
Hilarious! This reminds me of a StackOverflow question which asked the users for the weirdest language quirks they could think of. I laughed pretty hard at that too.
I understand the downvotes since this is HN and you people don't like useless comments here. But I simply thought it was very funny. No need to be mean here.
For the record, the downvotes aren't to be mean. We just don't want this turning into a Reddit clone and/or fill the comments with run-of-the-mill comments you'd see on any PHPbb forum.
No, I wasn't one of the ones who downvoted you and no, I'm not trying to be mean. There used to be times I wondered why an innocent comment of mine was downvoted and thought I'd just let you know instead of leave you hanging.
Thanks for the reply. Perhaps I was unclear. I completely understand the downvotes - that's why they're on the site. But I thought the "wow, you must be the best hacker ever congrats" comment was unnecessary and mean-spirited.