Skip to content

Instantly share code, notes, and snippets.

@jonathanmarvens
Last active December 19, 2015 06:39
Show Gist options
  • Save jonathanmarvens/5912859 to your computer and use it in GitHub Desktop.
Save jonathanmarvens/5912859 to your computer and use it in GitHub Desktop.
if (! Function.prototype.bind) {
Function.prototype.bind = function bind(context_this) {
if (Object.prototype.toString.call(this) === "[object Function]") {
var
arguments_trimmed = Array.prototype.slice.call(arguments, 1),
function_to_bind = this,
function_noop = function () {}
;
var function_bound = function () {
var arguments_all = arguments_trimmed.concat(Array.prototype.slice.call(arguments));
return function_to_bind.apply((
(this instanceof function_noop && context_this) ? this : context_this
), arguments_all);
};
function_noop.prototype = function_to_bind.prototype;
function_bound.prototype = new function_noop();
return function_bound;
} else {
throw new TypeError("Function.prototype.bind - the object that you passed in is not callable.");
}
};
}
Function.prototype.bind||(Function.prototype.bind=function(a){if("[object Function]"===Object.prototype.toString.call(this)){var b=Array.prototype.slice.call(arguments,1),c=this,d=function(){},e=function(){var e=b.concat(Array.prototype.slice.call(arguments));return c.apply(this instanceof d&&a?this:a,e)};return d.prototype=c.prototype,e.prototype=new d,e}throw new TypeError("Function.prototype.bind - the object that you passed in is not callable.")});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment