Last active
October 2, 2015 10:47
-
-
Save jordanthomas/2230240 to your computer and use it in GitHub Desktop.
bind()
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
// Have jQuery? | |
if ( !Function.prototype.bind ) { | |
Function.prototype.bind = function ( obj ) { | |
args = [].slice.call( arguments ); | |
args.unshift( this ); | |
return $.proxy.apply( this, args ); | |
}; | |
} |
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
if (!Function.prototype.bind) { | |
Function.prototype.bind = function (obj) { | |
// closest thing possible to the ECMAScript 5 internal IsCallable function | |
if (typeof this !== 'function') { | |
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); | |
} | |
var slice = [].slice, | |
args = slice.call(arguments, 1), | |
self = this, | |
nop = function () { }, | |
bound = function () { | |
return self.apply(this instanceof nop ? this : (obj || {}), | |
args.concat(slice.call(arguments))); | |
}; | |
bound.prototype = this.prototype; | |
return bound; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment