Skip to content

Instantly share code, notes, and snippets.

@ianpgall
Last active December 20, 2015 00:19
Show Gist options
  • Save ianpgall/6040514 to your computer and use it in GitHub Desktop.
Save ianpgall/6040514 to your computer and use it in GitHub Desktop.
JavaScript function that binds the context of a Function, returning a new one (detects native browser support)
var Bind = (function () {
"use strict";
var A, F, ret;
A = [];
F = function () { return undefined; };
if (F.bind && !F.hasOwnProperty("bind")) {
ret = function (func, thisArg /*, args */) {
var finalArgs = [thisArg];
A.push.apply(finalArgs, A.slice.call(arguments, 2));
return F.apply.call(func.bind, func, finalArgs);
};
} else {
ret = function (func, thisArg /*, args */) {
var finalArgs = A.slice.call(arguments, 2);
return function () {
var copiedArgs = A.slice.call(arguments, 0);
A.push.apply(finalArgs, copiedArgs);
func.apply(thisArg, finalArgs);
};
};
}
return ret;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment