I was recently alarmed to discover the following:
> {}+[]
0
> ({}+[])
"[object Object]"
> {}+[]+1
1
> ({}+[])+1
'[object Object]1'
> {}+[] == ({}+[])
false
Why does wrapping something in parenthesis change its type?
答案 0 :(得分:5)
{} + []
is an empty block followed by a an array with a unary +
operator, which is essentially, which is +[]
which is 0,
({} + [])
is a literal object plus a literal array, both get converted into strings, the string value of an object is "[object Object]"
plus the string value of an empty array which is ""
, hence the result you see.