Skip to content

Instantly share code, notes, and snippets.

@jonathanmarvens
Created October 26, 2013 18:09
Show Gist options
  • Save jonathanmarvens/7172689 to your computer and use it in GitHub Desktop.
Save jonathanmarvens/7172689 to your computer and use it in GitHub Desktop.
if (! Function.prototype.bind) {
Function.prototype.bind = function bind(context) {
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind: the object on which you're trying to bind to is not callable.")
}
var
bound_func,
func,
func_arguments,
noop_func
;
func = this;
func_arguments = Array.prototype.slice.call(arguments, 1);
noop_func = function () {};
bound_func = function () {
var
new_arguments
;
new_arguments = func_arguments.concat(Array.prototype.slice.call(arguments));
if (this instanceof noop_func && context) {
return func.apply(this, new_arguments);
} else {
return func.apply(context, new_arguments);
}
};
noop_func.prototype = this.prototype;
bound_func.prototype = new noop_func();
return bound_func;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment