You'll need the function to exist as a method of an object then you can invoke it dynamically with bracket notation []
and pass the name of the method as a string into the brackets as an index. You'll then use the spread operator ...
to flatten the parameter array into a delimited list.
Others have shown this with the global window
as the parent object, but (as we know) globals are generally bad, so just create your own.
Below, I am showing multiple methods and parameter choices and asking the user to type in which combination they want. Of course this input could be handled in many other ways.
// The methods must be stored in an object so that they can be accessed with
// bracket notation later.
let obj = {
method1: function foo1(p1, p2) {
console.log("method1 was called with: ", arguments);
},
method2: function foo1(p1, p2) {
console.log("method2 was called with: ", arguments);
},
params1: ['one', 'two'],
params2: ['three', 'four'],
};
// Get the user's desired invocation requirements:
let choice = prompt("Enter 'method1' or 'method2'");
let args = prompt("Enter 'params1' or 'params2'");
// Pass the method name (as a string) into the object to extract the
// value of that property. Then invoke it with () and look up the correct
// parameter property in the same way. Flatten the parameter array with the
// spread operator (...).
obj[choice](...obj[args]);