-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
精读《null >= 0?》 #36
Comments
@linhuiw js 作判断时,首先在两个值上调用 ToPrimitive coercion,如果两个调用的结果都不是 string,就会对两个值进行 ToNumber 强转为 number,然后进行数值比较。
null >= 0 // is null < 0 ?
+null < 0 // => 0 < 0 => false, so null >= 0 is true 让我们看一下为什么
再看看 至于下面的情况:
像 null >= {} // is null < {} => is null < NaN => is 0 < NaN === false
// so null >= {} === true? 正常来看 |
当比较两边类型不同时,先做类型转换,当类型相同时,如果是 >= 判断,会转成 < 判断。而 NaN 会无视类型转换直接给出 false 结果,因此很可能跳过了 < 判断。 |
@ascoders 说的不错,但是有点问题,[] 是转换成 "" 而不是 "0";对于允许隐式转换的比较操作,只要一个操作数是 NaN,那么这个表达式一定返回 false。 这是隐式转换表 |
学习了。 |
本期精读的文章是:null >= 0
看来大于等于号在 js 的执行逻辑并不符合直觉。
The text was updated successfully, but these errors were encountered: