I am looking for a way how to call a nested function when I have its name as string. I am writing a function that returns an object that will do some processing by calling functions that are part of its closure and are intentionally inaccessible from the global scope. The code goes something like this:
function makeHandler() {
function case1() {
...
}
function case2() {
...
}
let handler = {}
handler.handle = function(div) {
let divCase = div.getAttribute("data-case");
//divCase is now either "case1" or "case2". I want to call the correct function. I have:
({case1: case1, case2: case2}[divCase])()
}
return handler
}
let h = makeHandler()
h.handle(someDiv)
I find this approach unsatisfying, if I add case3
I will have to update the handle function. I'd think that the functions case1
and case2
must be part of some context, just like makeHandler
is part of the global context and accessible as property of the window
object.
I am aware of the very similar 10 year old question: Calling nested function when function name is passed as a string and I've seen other articles with similar recommendation to what I am doing -- create an object where properties are function names and values are functions themselves. I am looking for a more 'generic' solution.