title: Creating Multiple Instances of a Module
body: <p>I thought I was starting to understand JavaScript quite well but clearly not. Let me explain my problem with an example. First I have the following module defined:</p>
  
  <pre><code>var Test = function() {
      var counter = 0;
  
      function init() {
          alert(counter);
      }
  
      return {
          counter: counter,
          init: init
      }
  };
  </code></pre>
  
  <p>I then create 2 instances:</p>
  
  <pre><code>var test1 = new Test();
  var test2 = new Test();
  </code></pre>
  
  <p>Now I update the counter variable (as it is public) and do some alerts. So far so good.</p>
  
  <pre><code>alert(test1.counter); // Alerts 0
  test1.counter = 5;
  alert(test2.counter); // Alerts 0
  test2.counter = 10;
  alert(test1.counter); // Alerts 5
  </code></pre>
  
  <p>Now finally I say the following:</p>
  
  <pre><code>test1.init(); // Alerts 0
  test2.init(); // Alerts 0
  </code></pre>
  
  <p>This is the bit I don't understand. Why does this alert 0? I thought the first alert would be 5 and the second 10.</p>
  
  <p>I'd appreciate if someone could explain how the above could works or point me in the right direction. Thanks</p>