勘误
hhz-CN opened this issue · 2 comments
位置 :
一、值,类型和运算符
自动类型转换
错误内容 :
"字符串和数字转换为布尔值的规则表明,0,NaN和空字符串("")计为false,而其他所有值都计为true。"
原作中没有这一句 , 而且这句是错的 . NaN == false返回的是false .
这句话是存在的
http://eloquentjavascript.net/01_values.html
We can use this functionality as a way to fall back on a default value. If you have a value that might be empty, you can put
||
after it with a replacement value. If the initial value can be converted to false, you’ll get the replacement instead. The rules for converting strings and numbers to Boolean values state that0
,NaN
, and the empty string (""
) count asfalse
, while all the other values count astrue
. So0 || -1
produces-1
, and"" || "!?"
yields"!?"
.
再者,NaN
这个值比较特殊,当==
两边至少有一个NaN
时,不会转换,直接判定为false
。
转换指的是这个:
Boolean(NaN)
// false
new Boolean(NaN)
// Boolean {false}
这个在原文中也有说明:
There is only one value in JavaScript that is not equal to itself, and that is
NaN
(“not a number”).console.log(NaN == NaN) // → false