title: How can I conditionally define a function in javascript?
body: <p>Consider the following script:</p>
  
  <pre><code>if (false)​ {
      function foo() {
          alert("FOO");
      }
  }
  foo(); // alerts "FOO"
  </code></pre>
  
  <p>As you can see, the function <code>foo</code> is defined and works even though the condition for the <code>if</code> fails.</p>
  
  <p>The reason I want to do this is that I am developing an extension for InDesign and I made a script called utilities.jsx which holds various functions which are used in other scripts. In all the scripts that need these functions, I simply use:</p>
  
  <pre><code>app.doScript(File(scriptsFile.parent.fsName + "/t_utilities.jsx"), ScriptLanguage.JAVASCRIPT);
  </code></pre>
  
  <p>which defines all the functions I need and then I can use them. Now, since this script will be called multiple times by various files in an unpredictable order (the user decides), I thought I might only define the functions if they haven't been defined before (if utilities.jsx hasn't run yet). Kind of like <code>require_once</code> in php.</p>
  
  <p>I was thinking of something like the following, but that doesn't work as the functions are defined every time the script is run:`</p>
  
  <pre><code>var utilitiesHasRun;
  if (utilitiesHasRun !== true) {
    // define all the functions
    // ...
    utilitiesHasRun = true;
  }
  </code></pre>
  
  <p>Are there any other options?</p>