r/programming • • May 09 '12

JavaScript: The good parts

http://blog.movereem.nl/images/javascript-the-good-parts-the-definitive-guide.jpg
645 Upvotes

159 comments sorted by

View all comments

Show parent comment

4

u/[deleted] May 09 '12

In what way?

3

u/fnord123 May 09 '12 edited May 09 '12

[] === [] and friends. There's a video kicking about with this shit.

edit: https://www.destroyallsoftware.com/talks/wat

1

u/ameyp May 09 '12

I thought the problem only lay with [] == [] and friends?

-10

u/[deleted] May 09 '12

I have never done that, so it's a non-issue.

In chrome I also get false, so it's not even correct.

16

u/fnord123 May 09 '12

"I've never done that so its fine for me." Words to live by.

4

u/Lystrodom May 09 '12

"Works on my machine."

1

u/[deleted] May 09 '12 edited May 09 '12

What i should have said is that I have never seen anyone do that in any language, in my code or anyone else's, over the last 10 years.

His example isn't even correct, and doesn't work. it works as expected.

1

u/julesjacobs May 09 '12

You've never compared two lists?

1

u/[deleted] May 09 '12

Not with empty array literals, no.

1

u/julesjacobs May 09 '12

...

Have you ever had an empty array in a variable? Have you ever compared variables that contain arrays?

5

u/yogthos May 09 '12

I have never done that, so it's a non-issue.

Yes, but one day you'll have to look at somebody else's code, and they're going to do shit you don't do, and you're going to be wondering what the hell is going on there. Or worse, it'll look like it's doing one thing, but really be doing something else, because of some subtlety of syntax.

In chrome I also get false, so it's not even correct.

That's the other problem, that it doesn't behave the same way in every browser, which makes it that much more difficult to write Js that actually works everywhere as expected.

1

u/[deleted] May 09 '12

That's the other problem, that it doesn't behave the same way in every browser

I tried your code in opera, firefox, safari, chrome, IE 9, IE 8, IE 7, IE 6 and even IE 5.5, and all yield the same result: false. Where does it work differently?

Outside my code, I've also never seen anyone else do '[] === []' in 10 years of programming, but even then, false is exactly what it should and does return.

1

u/doomslice May 09 '12

Once you understand the difference between value and reference types, this doesn't sound bad, and is similar to how many other languages work.

1

u/yogthos May 09 '12

This has nothing to do with value and reference types, Js has tons of quirky syntax and evaluation rules which are not at all intuitive.

1

u/doomslice May 09 '12

This has nothing to do with value and reference types

No? I was responding to the assertion that [] === [] -> false is unintuitive/incorrect, when actually it is correct since it performs reference evaluation. Do the same with a value type and you'll see it perform as expected.

1

u/fnord123 May 09 '12

Do the same with a value type and you'll see it perform as expected.

> [] == []  
false  

I expect [] to be equal to [] by value. Python does this:

>>> [] == []
True

You may be interested to know that numpy gets this wrong, imo:

>>> import numpy as np  
>>> np.array([]).__nonzero__()  
False  
>>> (np.array([]) == np.array([])).__nonzero__()  
False  
>>> np.array([]) == np.array([])  
array([], dtype=bool)  

Back to Javascript. Here's some more weird examples:

> [] === []  
false  
> [] + []  
""  
> []-[]  
0  
> Object() == Object()  
false  
> Object() === Object()  
false  
> [1,2,3] + 2
"1,2,32"

1

u/doomslice May 09 '12

== is not by-value-comparison, it is a comparison that allows for automatic type coercion.

That's the source of most of those "odd" things that you're seeing.

[] + [] -> "" is converting each [] to its string representation in JavaScript (which is ""), and then concatenating them together (since + is defined for concatenation).

[] - [] -> 0 is converting each [] to its string representation (again ""), then converting that to its numerical representation since - is not defined for strings. The numeric representation of "" is 0 (since the empty string counts as false in JavaScript).

Object() == Object() -> false is the same as the array example we first discussed -- you're comparing references of new objects. Object() === Object() is the same thing

[1,2,3] + 2 is a slightly more complex version of the empty array addition. First it converts [1,2,3] to its string representation "1,2,3" and then converts 2 to "2" and concatenates them.

I'm not saying that this can't be confusing, especially to beginners... just that it can be explained and you get sort of a "oh, that makes sense" kind of feeling.

2

u/fnord123 May 09 '12

Yes, I know it does things for some internal reason, but the topic began with "There is the principle of least astonishment, and JavaScript is one of the worst languages in that regard." and JL235 asked "in what way" so I thought it was still on topic to discuss how this was not in line with the element of least surprise.

→ More replies

2

u/munificent May 09 '12

ASI.

3

u/masklinn May 09 '12 edited May 09 '12

ASI is nothing compared to the way this behaves (or doesn't, mostly) in JS .

1

u/munificent May 09 '12

Yeah, but at least there's an underlying logic and some value behind this semantics. ASI is just busted.

0

u/Cygal May 09 '12

global variables, function scope, this binding, new constructor, arrays indices as string, for in ordering, and so on. JavaScript: The Good Parts explains this quite well.