How to check if the string parameter passed in a function is too callable/function but not directly under window.
.
I know the open/ directly callable function can be checked using the syntax window['functionName']
But how about the member function declared inside an object to be checked?
In below example openFunction()
can be called but how to call obj1.foo()
?
Prefer not to use
eval()
Example Code:
var obj1 = {
foo: function() {
alert("I'm a function");
}
}
function openFunction() {
alert("I know i am easily callable");
}
function callSomeone(txtcallback) {
var fn = window[txtcallback];
if (typeof fn === 'function') {
fn();
}
console.log(typeof fn);
}
callSomeone('openFunction'); //function
callSomeone('obj1.foo'); //undefined