I'm having a difficulty calling a function inside of another function when its name is in a variable:
var obj = {}
obj.f = function() {
var inner = {
a: function() {
function b() {
alert('got it!');
}
b(); // WORKS AS EXPECTED
x = 'b';
[x](); // DOESN'T WORK, NEITHER this[x]() window[x](), etc.
}
}
inner.a();
}
obj.f();
I tried prefixing [x]()
with different scope paths but so far w/o success. Searching existing answers did not turn up anything. It works with this[x]()
if b()
is placed directly inside object inner. I would like to keep b()
as a function inside function a()
because of variable scope in function a()
, otherwise I would need to pass many parameters to b()
.
//// Re duplicate question: Quentin provided a more elegant answer in this thread imo.