-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Description
Unlike jQuery's $.each, Underscore has the good sense to give the passed-in callback the value first, followed by the key/index. This alone makes Underscore's _.each superior ... except for one minor detail.
Unlike just about every other for/each loop in existence (the JS native one, the jQuery one, etc.) Underscore's _.each does not have a way to explicitly break out of the loop. Actually, that's not technically correct: it does have just such a mechanism (the "breaker" variable), but the library selfishly reserves it for use by other Underscore functions only.
It doesn't have to be this way; if breaker was simply exposed as a property of the underscore object, then everyone could use the wonderful _.each function without any negative trade-offs. Imagine:
_.each([1,2,3], function(x) {
if (x == 2) return _.breaker; // or _.BREAKER?
console.log(x);
});
1
and all that would require is a single line of code to export breaker (actually not even that: just change "var breaker" to "_.breaker", move it down a few lines below the declaration of "var _" and call it a day).