Whereas using eval
is not a good programming practice. This question is for didactic nature, or to learn a better solution:
See the following example in Javascript:
var foo = foo || {};
foo.bar = function(str) { alert(str); };
foo.bar('aaa'); // trigger alert('aaa')
window['foo']['bar']('bbb'); // trigger alert('bbb')
I'm searching for an generic caller to work with foo.bar('str')
, foo.nestedObj.bar(params)
, foo.n2.n[1..99].bar(params)
Thats because I can't call something like:
param = [5,2,0];
call = 'foo.bar';
window[call](param); // not work
But I can call function using eval:
param = [5,2,0];
call = 'foo.bar'
eval(call + '(param)'); // works
How can I do this WITHOUT eval
?