Last active
December 18, 2015 14:38
-
-
Save imbcmdth/5798177 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
var hugeNum = Math.pow(2, 51); | |
function generateArguments(numberOfArgs) { | |
var letters = []; | |
while(numberOfArgs--) letters.push("arg_" + Math.ceil(Math.random() * hugeNum)); | |
return letters; | |
} | |
Function.prototype.partial = function partial() { | |
var fn = this; | |
var argumentsToApply = Array.prototype.slice.call(arguments); | |
var argumentsLeft = Math.max(0, fn.length - argumentsToApply.length); | |
var argList = generateArguments(argumentsLeft); | |
var functionCode = 'return function '; | |
functionCode += fn.name + '('; | |
functionCode += argList.join(', ') + ') {\n'; | |
functionCode += 'var args = Array.prototype.slice.call(arguments);\n'; | |
functionCode += 'args = suppliedArguments.concat(args);\n'; | |
functionCode += 'return fn.apply(this, args);\n'; | |
functionCode += '};' | |
return Function("suppliedArguments", "fn", functionCode)(argumentsToApply, this); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment