[英]Why {} == {} is false, but {} + {} == {} + {} is true
Why in node.js is {} == {}
equivalent to false
, but is {} + {} == {} + {}
equivalent to true
? 为什么node.js中
{} == {}
等于false
,但{} + {} == {} + {}
等同于true
?
> {} == {}
false
> {} + {} == {} + {}
true
+
here is the string-concatenation operator. +
这里是字符串连接运算符。 This: 这个:
{} == {}
means "if I create one object with {}
, and another object with {}
, are they the same object?"; 的意思是“如果我创建一个对象的
{}
和与另一个对象{}
它们是相同的对象?”; and the answer is "no". 答案是“不”。
This: 这个:
{} + {} == {} + {}
means "is the primitive string "[object Object][object Object]"
the same as the primitive string "[object Object][object Object]"
?"; 表示“是原始字符串
"[object Object][object Object]"
与原始字符串相同"[object Object][object Object]"
?”; and the answer is "yes". 答案是肯定的。
Edited to add: A number of commenters point out that in Chrome's Web Console, {} + {}
performs numeric addition, NaN + NaN
, such that {} + {} == {} + {}
actually returns false
(because it's not true that NaN == NaN
). 编辑添加:许多评论者指出,在Chrome的Web控制台中,
{} + {}
执行数字添加, NaN + NaN
,这样{} + {} == {} + {}
实际上返回false
(因为它不是确实NaN == NaN
)。 Firefox's Web Console gives the same result as Chrome's, but if you run this inside a page, it gives the same result as node.js. Firefox的Web控制台提供与Chrome相同的结果,但是如果您在页面内运行它,它会提供与node.js相同的结果。
[Redacted: long explanation of how the spec dictates that {} + {}
should be string concatenation and {} + {} == {} + {}
should be true; [编辑:对规范如何规定
{} + {}
应该是字符串连接以及{} + {} == {} + {}
应该是真的; the explanation, while correct, is no longer terribly interesting, given the below.] 鉴于以下情况,正确的解释不再是非常有趣的。]
Edited to add: Thanks to a comment by jJ' , I can now offer a better explanation of the inconsistency. 编辑补充:感谢jJ的评论,我现在可以更好地解释不一致性。
The reason for the Web Console's behavior is that the Web Console does not specifically require an expression; Web控制台行为的原因是Web控制台不特别需要表达式; it will happily accept something like
if(true) { }
. 它会愉快地接受
if(true) { }
。 So, when it sees {} + {}
, it doesn't interpret that as an expression; 因此,当它看到
{} + {}
,它不会将其解释为表达式; the initial {}
is interpreted as a naked block, then the + {}
is interpreted as an expression (creating a new object, converting it to a primitive number — namely NaN
— and evaluating to that number). 初始
{}
被解释为裸块,然后+ {}
被解释为表达式(创建一个新对象,将其转换为原始数字 - 即NaN
- 并评估该数字)。 The Web Console displays the result of the last expression (eg, the input 3; 4
will give the output 4
), which in this case is NaN
. Web控制台显示最后一个表达式的结果(例如,输入
3; 4
将给出输出4
),在这种情况下是NaN
。
{} + {} == {} + {}
, similarly, is interpreted as "empty block, followed by (+{}) == ({} + {})
", ie, "empty block, followed by NaN == '[object Object][object Object]'
", ie "empty block, followed by false
". 类似地,
{} + {} == {} + {}
被解释为“空块,后跟(+{}) == ({} + {})
”,即“空块,后跟NaN == '[object Object][object Object]'
“,即”空块,后跟false
“。
This can be fixed by using parentheses; 这可以通过使用括号来修复;
({} + {} == {} + {})
, for example, returns true
. ({} + {} == {} + {})
例如返回true
。
(This behavior, by the way, is not completely specific to the Web Console. eval
follows the same rules, such that eval('{} + {}')
is NaN
and eval('({} + {})')
is '[object Object][object Object]'
.) (顺便说一下,这种行为并不完全特定于Web控制台
eval
遵循相同的规则,例如eval('{} + {}')
是NaN
和eval('({} + {})')
是'[object Object][object Object]'
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.