Skip to content

Instantly share code, notes, and snippets.

@Cfeusier
Created September 9, 2015 01:22
Show Gist options
  • Select an option

  • Save Cfeusier/18803aea2b91572da2e6 to your computer and use it in GitHub Desktop.

Select an option

Save Cfeusier/18803aea2b91572da2e6 to your computer and use it in GitHub Desktop.
Polyfill for function.prototype.bind in coffeescript
if not Function.prototype.bind
Function.prototype.bind = (oThis) ->
if typeof @ is not 'function'
throw new TypeError 'Function.prototype.bind - what is trying to be bound is not callable'
aArgs = Array.prototype.slice.call arguments, 1
fToBind = @
fNOP = -> {}
fBound = ->
ctx = if @ instanceof fNOP then @ else oThis
innerArgs = Array.prototype.slice.call arguments
fToBind.apply ctx, aArgs.concat innerArgs
fNOP.prototype = @prototype if @prototype
fBound.prototype = new fNOP()
fBound
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment