Why is {}+[] different from ({}+[])? [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-29 08:13:44

问题


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?


回答1:


{} + [] 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.



来源:https://stackoverflow.com/questions/36409966/why-is-different-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!