Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in Jquery.each() and Array.prototype.forEach() method [duplicate]

Is there any difference between Jquery.each() and Array.prototype.forEach() method since array.forEach() method can also be used to loop over array-like objects with length property.The only difference i see is placement of arguments ,what else can be the difference in them?

 I found this:
 var obj = { one:1, two:2, three:3, four:4, five:5 };

jQuery.each(obj, function(i, val) {
  $("#" + i).append(document.createTextNode(" - " + val));
});

What i wan to know is ,do jquery.each() invokes function for object without length property??

like image 509
Maizere Pathak.Nepal Avatar asked Jul 05 '13 13:07

Maizere Pathak.Nepal


1 Answers

  • Placement of arguments in the callback.

  • Quantity of arguments in the callback (The .forEach() gives you get a reference to the original collection.)

  • Default this value in the callback. (In jQuery it's the current item, in .forEach() it's the JavaScript default)

  • Ability to manually set the this value in the callback. (jQuery doesn't give this option, .forEach() lets you via a third argument.)

  • Avoidance of non-defined properties on sparse Arrays. (The .forEach() avoids them, jQuery includes them.)


They're very differently behaving methods. jQuery's doesn't make any attempt to be compliant with or complimentary of the standard behaviors.

like image 195
5 revs, 3 users 88%user2437417 Avatar answered Sep 30 '22 17:09

5 revs, 3 users 88%user2437417