Skip to content

Instantly share code, notes, and snippets.

@tvolodimir
Last active December 14, 2015 14:49
Show Gist options
  • Save tvolodimir/5103173 to your computer and use it in GitHub Desktop.
Save tvolodimir/5103173 to your computer and use it in GitHub Desktop.
/*
faster bind by http://jsperf.com/bind-vs-emulate/9
*/
(function () {
var slice = Array.prototype.slice;
Function.prototype.bind2 = function (context) {
var f = this;
var curriedArgs = slice.call(arguments, 1);
if (curriedArgs.length) {
return function () {
var allArgs = curriedArgs.slice(0);
for (var i = 0, n = arguments.length; i < n; ++i) {
allArgs.push(arguments[i]);
}
f.apply(context, allArgs);
};
} else {
return createProxy(f, context);
}
};
function createProxy(f, context) {
return function bound() {
if (!(this instanceof bound)) {
f.apply(context, arguments);
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment