Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

javascript - underscore's each checking for {} return of callback

I was examining how UnderscoreJS implements their each/forEach

//somewhere up top:
var breaker = {};

//then the each function
var each = _.each = _.forEach = function (obj, iterator, context) {
    if (obj == null) return;
    if (nativeForEach && obj.forEach === nativeForEach) {
        obj.forEach(iterator, context);
    } else if (obj.length === +obj.length) {
        for (var i = 0, l = obj.length; i < l; i++) {
            if (iterator.call(context, obj[i], i, obj) === breaker) return;
        }
    } else {
        for (var key in obj) {
            if (_.has(obj, key)) {
                if (iterator.call(context, obj[key], key, obj) === breaker) return;
            }
        }
    }
};

//iterator = callback
//context  = optional third parameter of each to provide context in the callback
//obj      = the list
//key      = key of the object (i for index when an array)

Basically, it's executing the callback for each item in the object/array. But this like confuses me

if (iterator.call(context, obj[key], key, obj) === breaker) return;

From what I understand, if the callback returns an object, the loop breaks, but... Why it's comparing to breaker which is an internal object in the underscore module?. Doesn't it evaluate to false all the time since, even if the callback does return an object, it is always false since it's not the same object (therefore the loop never breaks). What's the reason behind this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

They use each internally for e.g. some. Since some does short-circuit, they can have each break there using the "secret" object, while not exposing this feature for regular users. They don't expose the break feature because the native function doesn't do that, either (so their shim is as native-like as possible). If they did, the break feature would only be available if the native function is unavailble, which isn't particularly helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...