Created
September 10, 2015 10:03
-
-
Save kesla/3452d667adf7ca18f2cd to your computer and use it in GitHub Desktop.
This file contains 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
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