Skip to content

Instantly share code, notes, and snippets.

@imbcmdth
Last active December 18, 2015 14:38
Show Gist options
  • Save imbcmdth/5798177 to your computer and use it in GitHub Desktop.
Save imbcmdth/5798177 to your computer and use it in GitHub Desktop.
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