I am new to JavaScript and am struggling with how reflection works there. What I want to do is: Given a String which is the function's name, invoke that function. E.g. var myString = "myFunctionName()" is given and then I want to invoke myFunctionName() using myString somehow.
Is this possible and how?
Thanks in advance!
It all depends on the context in which the function is declared.
For the browser's window context:
function test() {
console.log('Hello world!');
}
const string = 'test';
window[string]();
You can try:
function myFunctionName() {
// do something...
}
var myString = "myFunctionName()";
eval(myString)