Here is a line of code from underscore. What is that plus prefix for in this line?
if (obj.length === +obj.length) { // plus prefix?
This forces the value of obj.length
to be a Number
. Essentially this is done to make sure that the default length
value for an array-like object has not been overridden so that it can be iterated properly.
breaker
will do nothing in this context because even another empty object {}
will evaluate to false
when compared to breaker
.. even without an equivalence comparison.
However, breaker
is not used in that context because it is defined outside of the .each
function, which appears different than what you are showing here. Instead, it is used to force a "break" from other looping methods:
_.every = _.all = function(obj, iterator, context) {
/* snip */
if (!(result = result && iterator.call(context, value, index, list)))
return breaker;
You can see that if the result is not truthy in "every," we want to break immediately. _.every
calls _.each
, and it will return breaker
which will be true when compared to itself, allowing for an immediate break.