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 comments

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"
→ More replies (0)