by nodesocket · 14y ago · 0 comments
----- jsc ------
[] + []
> [] + {}
[object Object]
> {} + []
0
> {} + {}
NaN
------ node.js ------ > [] + []
''
> [] + {}
'[object Object]'
> {} + []
'[object Object]'
> {} + {}
'[object Object][object Object]' d8> ([] + [])
d8> ([] + {})
[object Object]
d8> ({} + [])
[object Object]
d8> ({} + {})
[object Object][object Object]
So I believe this is merely erroneous behavior of the console, not a weirdness of JavaScript itself.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 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?
Also, I'm on an old version of node, but my output matches jsc.
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.