Inside a function I need to test if another variable function exists and then call it if it does.
function div1_load(){
// do something
}
function main(id){
var func = id + '_load';
if (func is function) {
func.call();
}
}
main('div1');
I can think of two ways to test, but both involve eval() which I would like to avoid.
- typeof eval(func) === 'function'
- eval('typeof(' + func + ')') === 'function'
Is there a way without using eval() ? If not, which of my two methods is most secure ?