Last active
December 20, 2015 00:19
-
-
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)
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
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