Skip to content

Instantly share code, notes, and snippets.

@kevireilly
Created May 9, 2015 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevireilly/ff783717bb9cd0b11790 to your computer and use it in GitHub Desktop.
Save kevireilly/ff783717bb9cd0b11790 to your computer and use it in GitHub Desktop.
Polyfill for Function.prototype.bind
(function(){
if (!Function.prototype.bind) {
var Empty = function(){};
Function.prototype.bind = function bind(that) { // .length is 1
var target = this;
if (typeof target !== "function") {
throw new TypeError("Function.prototype.bind called on incompatible " + target);
}
var args = Array.prototype.slice.call(arguments, 1); // for normal call
var binder = function () {
if (this instanceof bound) {
var result = target.apply(
this,
args.concat(Array.prototype.slice.call(arguments))
);
if (Object(result) === result) {
return result;
}
return this;
} else {
return target.apply(
that,
args.concat(Array.prototype.slice.call(arguments))
);
}
};
var boundLength = Math.max(0, target.length - args.length);
var boundArgs = [];
for (var i = 0; i < boundLength; i++) {
boundArgs.push("$" + i);
}
var bound = Function("binder", "return function(" + boundArgs.join(",") + "){return binder.apply(this,arguments)}")(binder);
if (target.prototype) {
Empty.prototype = target.prototype;
bound.prototype = new Empty();
// Clean up dangling references.
Empty.prototype = null;
}
return bound;
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment