Comments (5)
@ascoders 说的不错,但是有点问题,[] 是转换成 "" 而不是 "0";对于允许隐式转换的比较操作,只要一个操作数是 NaN,那么这个表达式一定返回 false。
这是隐式转换表
from weekly.
当比较两边类型不同时,先做类型转换,当类型相同时,如果是 >= 判断,会转成 < 判断。而 NaN 会无视类型转换直接给出 false 结果,因此很可能跳过了 < 判断。
from weekly.
按文中说的,
if null < 0 is false, then null >= 0 is true;
那可以理解 为什么 null >= 0 了.
同理 {} >= {} 也是 true;
{} < {} // false
{} >= {} // true
可是那为啥
null == {} // false
null < {} // false
null >= {} //false
from weekly.
@linhuiw js 作判断时,首先在两个值上调用 ToPrimitive coercion,如果两个调用的结果都不是 string,就会对两个值进行 ToNumber 强转为 number,然后进行数值比较。
if null < 0 is false, then null >= 0 is true;
null >= 0 // is null < 0 ?
+null < 0 // => 0 < 0 => false, so null >= 0 is true
让我们看一下为什么 null >= [] === true
?
null >= [] // is null < []? => 将右边转换为 0 => 将 null 转换为 0 => 0 < 0 显然是 false
// so null >= [] === true
Tip: 感谢 @mrcooder 的提示,此处答案已修正
再看看 null >= [1]
呢?答案是 false,最终会进行 0 < 1
的比较,因此反过来就是 false。
至于下面的情况:
null >= {} is false;
像 []
''
'0'
false
强转 ToNumber
后值为 0,而 Number({})
的结果为 NaN
。
null >= {} // is null < {} => is null < NaN => is 0 < NaN === false
// so null >= {} === true?
正常来看 null >= {}
结果应该是 true,但结果却是 false。我估计因为 NaN 在 js 中是个特殊的存在,所以直接判定为 false,而没有进行反转判断。
from weekly.
学习了。
from weekly.
Related Issues (20)
- 可视化搭建 - 场景实战
- 加班中,请假一次 HOT 1
- 【自荐开源】AI可视化SolidUI HOT 2
- 精读《自由 + 磁贴混合布局》
- 请假一次 🏳️
- 精读《自由布局吸附线的实现》
- 请假一次 HOT 1
- 精读《算法题 - 通配符匹配》
- 这里我想是对应的 '*' 不匹配任何字符? HOT 1
- 精读《算法题 - 统计可以被 K 整除的下标对数目》
- 精读《算法题 - 最小覆盖子串》
- 请假一次
- 精读《算法题 - 地下城游戏》
- 请假一次 HOT 6
- 精读《VisActor 数据可视化工具》
- 精读《算法题 - 编辑距离》
- 请假一次
- 精读《算法题 - 二叉树中的最大路径和》
- 休刊一段时间 HOT 8
- 真的牛逼,竟然检测有病毒
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from weekly.