Last active
December 24, 2015 15:39
-
-
Save lutzissler/6822599 to your computer and use it in GitHub Desktop.
jQuery implementation of ECMAScript 5’s reduce() method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.reduce = function(fnReduce, initialValue) { | |
var values = this, | |
previousValue = initialValue; | |
values.each(function(index, currentValue) { | |
previousValue = fnReduce.call(currentValue, previousValue, currentValue, index, values); | |
}); | |
return previousValue; | |
}; |
Thanks for your feedback! It reduces the current matched set, which is why it’s attached to $.fn
. The change to call
is definitely worth the effort though, thanks for pointing this out.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You shouldn't attach it to
$.fn
as it doesn't operate on the DOM. Besides, there's no need forfnReduce.apply
, for your use casecall
works better.