I have a function where I want to pass in the name of the member function it should execute:
const gen = (N, inFunc) => { //inFunc = 'func1' or 'func2'
const func1 = () => {...}
const func2 = () => {...}
let func = this[inFunc] // doesn't work; no 'this' in an ES6 module
newFunc = func(); // nice try
}
Any solutions?
That should work:
const gen = (N, inFunc) => {
const fns = {
func1: () => {...}
func2: () => {...}
}
let func = fns[inFunc]
newFunc = func();
}
yep, this wasn't going to help with that ;-)
Konrad Linkowski's answer is fine, but I went a different route (suggested by barmar):
const gen = (N, inFunc) => { //inFunc = 'func1' or 'func2'
const func1 = () => {...}
const func2 = () => {...}
const funcs = {
func1, func2 }
let newFunc = funcs[inFunc]();
}