Why is it that the $.each utility method doesn't exit the loop on
return false when iterating over objects but does when iterating over
arrays? Here's a simple example that requires Firebug:

$.each([1,2,3], function() {
  console.log('outer - ', arguments);
  $.each([4,5], function() {
    console.log('inner - ', arguments);
    return false;
  });
  return false;
});

vs.

$.each({foo:'bar',baz:'quux'}, function() {
  console.log('outer - ', arguments);
  $.each({ichi:'ni',san:'shi'}, function() {
    console.log('inner - ', arguments);
    return false;
  });
  return false;
});

As you should see when iterating over objects return false has no
effect on the loop and console.log() is called 6 times (versus 2 when
iterating over arrays).

I know how this is happening as it's pretty clear in the the jQuery
code[1] but I'm now wondering why it's happening. Is it a bug in the
code, the documentation or some kind of arcane feature? Any
enlightenment on the matter would be most appreciated.

Cheers,
Dan

[1] It checks for the length property on the object it is about to
iterate over and if doesn't exist a plain "for in" loop is used,
otherwise an iterative loop is used which checks return value of the
callback and breaks from the loop if it is false.

Reply via email to