(Hoping I've got the terminology right...)
My code, much simplified:
function foo(parm1, fn){
// do stuff with parm1, and then...
window[fn]();
}
function bar(){
// do the other thing
}
Which is then invoked as:
foo('some string', 'bar');
I'd like to use a function expression(?), like so:
foo('some string', function(){ // do the other thing });
while retaining the option to pass a function name as in the first example for when what 'bar' has to do is many steps. I've tried
function foo(parm1, fn){
// do stuff with parm1, and then...
if(typeof fn != 'function'){
window[fn]();
} else {
return true;
}
}
foo('some string', function(){ // but this never fires });
Can I have it both ways?
You can. You forgot to call fn
if it is a function:
if(typeof fn != 'function'){
window[fn]();
} else {
fn(); // fn is (probably) a function so lets call it
}