Revision 9b4a3b97-ce83-4643-b836-5c3c8d6e1447 - Stack Overflow
What you've *literally* asked for is impossible without using `eval` unless you want the function to be global. If you do, you can do this:
// Creates global function
window[functionName] = function() { };
Note that that function won't actually have a *name* (in call stacks and such), but you can use the name in the variable to call it. So for example:
var functionName = "foo";
window[functionName] = function() {
console.log("I got called");
};
foo(); // Outputs "I got called"
That's just a specific case of the more general purpose concept of using an object property. Here's a non-global function attached to an object property:
var functionName = "foo";
var obj = {}; // The object
obj[functionName] = function() { }; // The function
obj[functionName] = function() {
console.log("I got called");
};
obj.foo(); // Outputs "I got called"
In both cases, it works because in JavaScript, you can refer to a property either using dotted notation and a *literal* property name (`obj.foo`), or using bracketed notation and a *string* property name (`obj["foo"]`). In the latter case, of course, you can use any expression that evaluates to a string.