Created
July 20, 2012 03:51
-
-
Save Paxa/3148532 to your computer and use it in GitHub Desktop.
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.prototype.bind = function(that){ | |
var self = this, | |
args = arguments.length > 1 ? Array.slice(arguments, 1) : null, | |
F = function(){}; | |
var bound = function(){ | |
var context = that, length = arguments.length; | |
if (this instanceof bound){ | |
F.prototype = self.prototype; | |
context = new F; | |
} | |
var result = (!args && !length) | |
? self.call(context) | |
: self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments); | |
return context == that ? result : context; | |
}; | |
return bound; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment