Skip to content

Instantly share code, notes, and snippets.

@arian
Forked from ibolmo/Function.bind.js
Created August 3, 2011 14:04
Show Gist options
  • Save arian/1122716 to your computer and use it in GitHub Desktop.
Save arian/1122716 to your computer and use it in GitHub Desktop.
// 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;
};
@Inviz
Copy link

Inviz commented Aug 3, 2011

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment