Last active
June 3, 2016 17:43
-
-
Save suguru03/f59435e22a33754d3705d880a790aec5 to your computer and use it in GitHub Desktop.
create function with name
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
function createFunc(name, args, fstr) { | |
var arr = [name, args, fstr]; | |
var str = 'return function %s(%s) %s;'; | |
str = str.replace(/%s/g, function() { | |
return arr.shift(); | |
}); | |
return new Function(str)(); | |
} | |
var name = 'test'; | |
var args = ['a', 'b']; | |
var fstr = '{ return a + b; }'; | |
var func = createFunc(name, args, fstr); | |
console.log(func(1, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment