4 ms·
I'll put this here just in case no one comments. https://lodash.com/docs#forEach https://lodash.com/docs#forEach http://underscorejs.org/#each http://undersco
by kevindeasis 11y ago
I'll put this here just in case no one comments.
https://lodash.com/docs#forEach https://lodash.com/docs#forEach
http://underscorejs.org/#each http://underscorejs.org/#each
- johnhenry 11y agoIf I'm correct, you're showing this as a counter example to prove that the two have incompatible APIs and are thus LoDash can't be used ad a drop in replacement. However, Lodash does alias it's 'forEach' function with 'each', so this wouldn't be an issue?
- tdumitrescu 11y agoYeah, each and forEach are aliases in both lodash and underscore.
- ksherlock 11y agounderscore.each has an optional 3rd parameter (context). lodash.each does not. If you depend on that 3rd parameter, you will be in a world of pain if you drop-in lodash. In fact you'd be better off if there was no alias because you would catch the problem sooner and have to think about the API differences when fixing it.
- 9point6 11y agoTo be fair, that context parameter has only just been removed as part of 4.0.0. Instead they recommend you change your code to be of the form: _.each(collection, _.bind(function, context)) Which actually seems a lot more correct than some arbitrary context parameter
- vkjv 11y agoHow is it arbitrary? It's part of the ES spec. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... I'm honestly curious why this is being removed?
- 9point6 11y agoAh, I actually didn't realise this was the case, I've always used `.bind(this)` for this kind of thing.