Skip to content

Instantly share code, notes, and snippets.

@jordanthomas
Last active October 2, 2015 10:47
Show Gist options
  • Save jordanthomas/2230240 to your computer and use it in GitHub Desktop.
Save jordanthomas/2230240 to your computer and use it in GitHub Desktop.
bind()
// Have jQuery?
if ( !Function.prototype.bind ) {
Function.prototype.bind = function ( obj ) {
args = [].slice.call( arguments );
args.unshift( this );
return $.proxy.apply( this, args );
};
}
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