Possible Duplicate:
What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?可能重复:CodeMash 2012的“Wat”演讲中提到的这些奇怪的JavaScript行为的解释是什么?
Could someone explain to me how thees returning results can be different in javascript:
有人可以向我解释,在javascript中,返回结果的结果会有所不同:
› {} + []
» 0
› [] + {}
» "[object Object]"
1 个解决方案
#1
7
This is because the {}
in the code is not an object literal, but an empty block.
这是因为代码中的{}不是对象文字,而是空块。
It is parsed as:
它被解析为:
{}; // empty block
+ []; // this result is shown in the console
Compare with ({}) + []
which yields the same results as [] + {}
; in this case the parenthesis force the {}
to be treated/parsed "in an expression context".
与({})+ []比较,产生与[] + {}相同的结果;在这种情况下,括号强制{}在表达式上下文中处理/解析“。
There are a bunch of duplicates on SO about this particular dual-nature of {}
(as an expression or block?) but, finding them can be somewhat tricky ..
有关SO的这种特殊的双重性质(作为表达式或块?)有一堆重复,但是找到它们可能有些棘手......
I found https://meta.stackexchange.com/questions/83911/how-do-i-search-*-for-at-keywords-like-private-or-synthesize on Meta, and using Symbolhound the "closest" duplicates I could find resolved around questions like this (that relate to the need to add parenthesis when "eval'ing JSON") or this (where the use of constructs like "{} == false" is a syntax error).
我在Meta上找到了https://meta.stackexchange.com/questions/83911/how-do-i-search-*-for-at-keywords-like-private-or-synthesize,并使用了Symbolhound“最接近的”重复项我可以找到解决这样的问题(与“eval'ing JSON”时需要添加括号)或者这(使用像“{} == false”这样的结构是语法错误)。
If anyone knows a better way to search SO for this sort question, or has a link to such a duplicate handy ..
如果有人知道一个更好的方法来搜索SO这个问题,或者有一个链接到这样的重复方便..
#1
7
This is because the {}
in the code is not an object literal, but an empty block.
这是因为代码中的{}不是对象文字,而是空块。
It is parsed as:
它被解析为:
{}; // empty block
+ []; // this result is shown in the console
Compare with ({}) + []
which yields the same results as [] + {}
; in this case the parenthesis force the {}
to be treated/parsed "in an expression context".
与({})+ []比较,产生与[] + {}相同的结果;在这种情况下,括号强制{}在表达式上下文中处理/解析“。
There are a bunch of duplicates on SO about this particular dual-nature of {}
(as an expression or block?) but, finding them can be somewhat tricky ..
有关SO的这种特殊的双重性质(作为表达式或块?)有一堆重复,但是找到它们可能有些棘手......
I found https://meta.stackexchange.com/questions/83911/how-do-i-search-*-for-at-keywords-like-private-or-synthesize on Meta, and using Symbolhound the "closest" duplicates I could find resolved around questions like this (that relate to the need to add parenthesis when "eval'ing JSON") or this (where the use of constructs like "{} == false" is a syntax error).
我在Meta上找到了https://meta.stackexchange.com/questions/83911/how-do-i-search-*-for-at-keywords-like-private-or-synthesize,并使用了Symbolhound“最接近的”重复项我可以找到解决这样的问题(与“eval'ing JSON”时需要添加括号)或者这(使用像“{} == false”这样的结构是语法错误)。
If anyone knows a better way to search SO for this sort question, or has a link to such a duplicate handy ..
如果有人知道一个更好的方法来搜索SO这个问题,或者有一个链接到这样的重复方便..