-
-
Save arian/1122716 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
// http://jsperf.com/function-proto-bind/3 | |
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
You dont need to create F function every time. You can create function once, and then set prototype and construct the function and the prototype will be frozen for that instance. It's like 3 times faster.
I know that it also applies to reset() function of mootools Class