Skip to content

Instantly share code, notes, and snippets.

@kesla
Created September 10, 2015 10:03
Show Gist options
  • Save kesla/3452d667adf7ca18f2cd to your computer and use it in GitHub Desktop.
Save kesla/3452d667adf7ca18f2cd to your computer and use it in GitHub Desktop.
function fullName (firstName, secondName) {
return firstName + ' ' + secondName;
}
function fullName2(obj) {
return obj.firstName + ' ' + obj.secondName;
}
console.log(beepboop(fullName2, ['firstName', 'secondName'])('David', 'Hipsterson'));
console.log(fullName('David', 'Hipsterson'));
function beepboop (fn, argNames) {
var functionBody = [
'var obj = {};'
].concat(argNames.map(function (argName, index) {
return 'obj["' + argName + '"] = arguments[' + index + ']';
}).concat([
'return (' + fn.toString() + ')(obj)'
])).join('\n');
console.log(functionBody);
return new Function (argNames, functionBody);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment