为什么{} + []与({} + [])不同? [英] Why is {}+[] different from ({}+[])?

查看:189
本文介绍了为什么{} + []与({} + [])不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近惊慌地发现以下内容:

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,

{} + [] is an empty block followed by a an array with a unary + operator, which is essentially, which is +[] which is 0,

({} + [])是一个文字对象加上一个文字数组,两者都是转换为字符串,对象的字符串值是[object Object]加上空数组的字符串值,因此你看到的结果。

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

这篇关于为什么{} + []与({} + [])不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆