When, for example:
var obj = {
0: 'first',
length: '1'
};
alert(obj.length === +obj.length);
Underscore's each
is a generic, therefore can work with other objects other than an array
. Just like ECMA5 forEach
The forEach function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether the forEach function can be applied successfully to a host object is implementation-dependent.
So underscore are checking the validity of an object's length
property. And they deem an object arrayLike
, for that method of iteration, only if the object's length
returns a number
which is not NaN
, and is certainly not a string
. So in my above example, obj
would fall through to their keys
iteration, if there is no native/polyfilled forEach
.