subrayar cada comprobación para {} devolución de devolución de llamada
Estaba examinando cómo UnderscoreJS implementa suseach
/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)
Básicamente, está ejecutando la devolución de llamada para cada elemento en el objeto / matriz. Pero esto me confunde
if (iterator.call(context, obj[key], key, obj) === breaker) return;
Por lo que entiendo, si la devolución de llamada devuelve un objeto, el bucle se rompe, pero ...¿Por qué se compara conbreaker
¿Cuál es un objeto interno en el módulo de subrayado?. ¿No evalúa afalse
todo el tiempo desde entonces, incluso si la devolución de llamada devuelve un objeto, siempre esfalse
ya que no es el mismo objeto (por lo tanto, el buclenunca se rompe). ¿Cuál es la razón detrás de esto?