Skip to content

Instantly share code, notes, and snippets.

@nirname
Last active December 11, 2015 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nirname/4563074 to your computer and use it in GitHub Desktop.
Save nirname/4563074 to your computer and use it in GitHub Desktop.
Inject method in CoffeSrcipt using JQuery
# inject function
inject = (collection, object, handler)->
$.each collection, (index, value)->
object = handler(object, value)
true
object
# the same as a method
inject_method = (object, handler)->
inject(this, object, handler)
# iterating over jquery object
$.fn.inject = inject_method
# iterating over arrays
if Array.prototype.inject = undefined
Array.prototype.inject = inject_method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment