This question already has an answer here:
- Dynamic function name in javascript? 18 answers
How to create a function with a dynamic name? Something like:
function create_function(name){
new Function(name, 'console.log("hello world")');
}
create_function('example');
example(); // --> 'hello world'
Also the function should be a Function Object so I can modify the prototype of the object.
I've been playing around with this for the last 3 hours and finally got it at least somewhat elegant using new Function as suggested on other threads:
If you run the above code, you should see the following output to your console:
or
or