Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
75
rated 0 times [  79] [ 4]  / answers: 1 / hits: 21371  / 12 Years ago, tue, december 11, 2012, 12:00:00

These two functions, _.forEach and $.each, taken respectively from underscore and jQuery seems to make the same thing.



What are the possible reasons to prefer one implementation to the other?


More From » jquery

 Answers
24

_.forEach and $.each differs for the arguments passed to the callback.



If you use _.forEach, the first argument passed to the callback is the value, not the key.

So if you don't bother at all about the key you should use _.forEach.



Other differences:




  1. _.forEach it will be a bit faster as it uses the native Arrray.prototype.forEach in modern browsers.

  2. the this value is also different. jQuery's behavior doesn't follow the standard, underscore's does.


[#81487] Monday, December 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andrewb

Total Points: 259
Total Questions: 124
Total Answers: 90

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;