Created
October 26, 2013 18:09
-
-
Save jonathanmarvens/7172689 to your computer and use it in GitHub Desktop.
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
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