title: Sharing JS functions between files without making them global or attaching them to a global object?
body: <p>Say that I've got a function that I want to re-use between multiple files. I could just make it global but that's no good.</p>
  
  <p><strong>master_file.js</strong></p>
  
  <pre><code>add = function(num1, num2){
    return num1 + num2;
  };
  
  subtract = function(num1, num2){
    return num1 - num2
  };
  </code></pre>
  
  <p><strong>file1.js</strong></p>
  
  <pre><code>add(4,4);
  </code></pre>
  
  <p><strong>file2.js</strong></p>
  
  <pre><code>add(9,1);
  </code></pre>
  
  <p><strong>file3.js</strong></p>
  
  <pre><code>subtract(8,2);
  </code></pre>
  
  <p>Instead, I can create a global object and attach the functions as values of the global object (or the <code>window</code> object as others do). </p>
  
  <p><strong>master_file_v2.js</strong></p>
  
  <pre><code>var add = function(num1, num2){
    return num1 + num2;
  };
  
  var subtract = function(num1, num2){
    return num1 - num2
  };
  
  global = {
  
    add: function(num1, num2){
      return add(num1, num2);
    },
    subtract: function(num1, num2){
      return subtract(num1, num2);
    }
  
  };
  </code></pre>
  
  <p>Then I would have to call the functions like so.</p>
  
  <p><strong>file1.js</strong></p>
  
  <pre><code>global.add(4,4);
  </code></pre>
  
  <p><strong>file2.js</strong></p>
  
  <pre><code>global.add(9,1);
  </code></pre>
  
  <p><strong>file3.js</strong></p>
  
  <pre><code>global.subtract(8,2);
  </code></pre>
  
  <p>Is there a way to <em>not</em> call the functions like this? I would prefer to directly call them by their names just like before, but without declaring them as global.</p>
  
  <p><strong>file1.js</strong></p>
  
  <pre><code>add(4,4);
  </code></pre>
  
  <p><strong>file2.js</strong></p>
  
  <pre><code>add(9,1);
  </code></pre>
  
  <p><strong>file3.js</strong></p>
  
  <pre><code>subtract(8,2);
  </code></pre>