title: javascript: passing a function name vs. IIFE
body: <p>(Hoping I've got the terminology right...)</p>
  
  <p>My code, much simplified:</p>
  
  <pre><code>function foo(parm1, fn){
      // do stuff with parm1, and then...
      window[fn]();
  }
  
  function bar(){
      // do the other thing
  }
  </code></pre>
  
  <p>Which is then invoked as:</p>
  
  <pre><code>foo('some string', 'bar');
  </code></pre>
  
  <p>I'd like to use a function expression(?), like so:</p>
  
  <pre><code>foo('some string', function(){ // do the other thing });
  </code></pre>
  
  <p>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</p>
  
  <pre><code>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 });
  </code></pre>
  
  <p>Can I have it both ways?</p>