javascript - Why doesn't underscore validate its functions? -
particularly in implementation of _.each
. seems simple typeof iterator === 'function'
trick. possibly or obj == null
check.
// cornerstone, `each` implementation, aka `foreach`. // handles objects built-in `foreach`, arrays, , raw objects. // delegates **ecmascript 5**'s native `foreach` if available. var each = _.each = _.foreach = function(obj, iterator, context) { if (obj == null) return obj; if (nativeforeach && obj.foreach === nativeforeach) { obj.foreach(iterator, context); } else if (obj.length === +obj.length) { (var = 0, length = obj.length; < length; i++) { if (iterator.call(context, obj[i], i, obj) === breaker) return; } } else { var keys = _.keys(obj); (var = 0, length = keys.length; < length; i++) { if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return; } } return obj; };
there no need validation. if input function incorrect, caused programming error , should treated such blowing up.
Comments
Post a Comment