Created
May 9, 2015 00:25
-
-
Save kevireilly/ff783717bb9cd0b11790 to your computer and use it in GitHub Desktop.
Polyfill for Function.prototype.bind
This file contains hidden or 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
(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