Created
September 9, 2015 01:22
-
-
Save Cfeusier/18803aea2b91572da2e6 to your computer and use it in GitHub Desktop.
Polyfill for function.prototype.bind in coffeescript
This file contains hidden or 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
| 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