if(obj.length === +obj.length)

underscore源码里面这样写的原因是什么呢?

上网找了一下几乎都是复制粘贴,讲的还有明显错误。在stackoverflow上找到一个觉得比较合理的答案:https://stackoverflow.com/questions/9188998/obj-length-obj-length-in-javascript

(obj.length === +obj.length)效果其实等同于(typeof obj.length == 'number'),(主要是为了区别对象和数组,对象一般没有length属性,属性字符串,方法等有)至于为什么采用看起来没那么直观的前一种表达方法呢,也许是为了在压缩成mini版的时候节省5个字节

(a.length===+a.length) VS (typeof a.length=='number')

猜你喜欢

转载自xiaoxiaoher.iteye.com/blog/2378371