Skip to content

Instantly share code, notes, and snippets.

@bfricka
Created April 6, 2013 03:15
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 bfricka/5324578 to your computer and use it in GitHub Desktop.
Save bfricka/5324578 to your computer and use it in GitHub Desktop.
Bind Polyfill in Coffeescript
unless Function::bind
Function::bind = (oThis) ->
# closest thing possible to the ECMAScript 5 internal IsCallable function
if typeof this isnt "function"
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable")
aArgs = Array::slice.call(arguments, 1)
fToBind = this
fNOP = ->
fBound = ->
fToBind.apply (if this instanceof fNOP then this else oThis), aArgs.concat(Array::slice.call(arguments))
fNOP:: = @::
fBound:: = new fNOP()
fBound
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment