An interesting JS implicit conversion problem
Print an expression in the Chrome console
[] + {} //结果为 [object object]
Then adjust the order to print
{} + [] //结果为 0
Then combine the two expressions
{} + [] === [] + {} //true
WTF???
The principle is explained as follows:
General type conversion, face +, first call valueof^[1] to convert, if the result of conversion is not
Primitive type, then the ToString () method is used to convert, so
[]+{} //结果Wie [object + object]
But {} in JS or in many languages is the code block of the difference between the symbol, so in the first place, will be considered blank block and
Ignore , so +[], ie + "", the final result will be 0
{}+[]// 0
and the final
{}+[] === [] + {}//true 是因为 chrome 默认在这种判断的外边增加了 括号, 所以两个字符//"[object object]" === "[object object]" 自然就是 true 了.
[1] except date
An interesting JS implicit conversion problem