Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在 Underscore.js 的 _.each 实现中遇到了这行代码,我很好奇这里发生了什么。obj 前面的“+”有什么作用?
if (obj.length === +obj.length) { ... }
if测试是数字而obj.length不是NaN. 右边总是一个数字(或者NaN如果obj.length不能被解释为一个数字)。如果也是一个数字,它只会===在左侧。obj.length
if
obj.length
NaN
===
请注意,如果是看起来像数字的字符串,则 usingisNaN将不起作用;obj.length也就是说,isNan("3")返回false。还要注意的NaN === NaN是false——NaN绝不===是任何东西。
isNaN
isNan("3")
false
NaN === NaN