title: Check if JavaScript function returns without executing it?
body: <p>I have a given function that takes, among other arguments, two optional arguments which may be functions. Both need to be optional, one will be a function, and one will either be a boolean or a function that returns a boolean.</p>
  
  <pre><code>// Obj.func(variable, String[, Object][, Boolean||Function][, Function]);
  Obj.func = function(other, assorted, args, BoolOrFunc, SecondFunc) {
      // execution
  };
  
  Obj.func(
      [
          'some',
           'data'
      ],
      'of varying types',
      {
          and: 'some optional arguments'
      },
      function() {
          if(some condition) {
              return true;
          }
          return false;
      },
      function() {
         // do things without returning
      }
  );
  </code></pre>
  
  <p>It is my desire that both functions (and several of the other arguments, if it matters) be optional, which means I have code in the function to determine which arguments the user intended to use.</p>
  
  <p>Unfortunately, as both may be functions, and may be specified directly in the function call, I cannot simply use <code>typeof</code> or <code>instanceof</code> conditionals. However, since the first function, if it exists, will <em>always</em> return a boolean (and the second function will not return at all), one idea I had would be to check its return value:</p>
  
  <pre><code>if(typeof BoolOrFunc === 'boolean'
     || (typeof BoolOrFunc === 'function' &amp;&amp; typeof BoolOrFunc() === 'boolean')) {
      // BoolOrFunc is either a boolean or a function that returns a boolean.
      // Handle it as the intended argument.
  } else {
      // Otherwise, assume value passed as BoolOrFunc is actually SecondFunc,
      // and BoolOrFunc is undefined.
  }
  </code></pre>
  
  <p>This works in principle; however, running <code>typeof BoolOrFunc()</code> executes the function, which causes a problem if the function does more than just return a boolean: that is, if the function passed as <code>BoolOrFunc</code> is <em>actually</em> meant to be <code>SecondFunc</code>. <code>SecondFunc</code>, in this case, is something of a callback function, and may perform actions, including DOM modifications, that I don't want to execute immediately.</p>
  
  <p>For this reason, my question is: <strong>Is there a way to check if a function returns without executing it?</strong></p>
  
  <p>One thing I had considered was to call <code>BoolOrFunc.toString()</code>, then perform a Regular Expression search for the return value, something along the lines of…</p>
  
  <pre><code>if(typeof BoolOrFunc === 'boolean'
     || (typeof BoolOrFunc === 'function'
         &amp;&amp; BoolOrFunc.toString().search(/return (true|false);/) !== -1)) {
      // BoolOrFunc is either a boolean or contains a return string with a boolean.
      // Handle it as the intended argument.
  }
  </code></pre>
  
  <p>Note that the above code may not work as written: I've not actually built a test case for it, because, well, it seems exceptionally inefficient and potentially unreliable, and I figured someone here might have a more elegant solution to my quandary. That having been said, I figured I'd include it for discussion purposes.</p>