title: How can I count the instances of an object?
body: <p>If i have a Javascript object defined as:  </p>
  
  <pre><code>function MyObj(){};
  
  MyObj.prototype.showAlert = function(){
     alert("This is an alert");
     return;
  };
  </code></pre>
  
  <p>Now a user can call it as:</p>
  
  <pre><code>var a = new MyObj();
  a.showAlert();
  </code></pre>
  
  <p>So far so good, and one can also in the same code run another instance of this:</p>
  
  <pre><code>var b = new MyObj();
  b.showAlert();
  </code></pre>
  
  <p>Now I want to know, how can I hold the number of instances MyObj?
  is there some built-in function?</p>
  
  <p>One way i have in my mind is to increment a global variable when MyObj is initialized and that will be the only way to keep track of this counter, but is there anything better than this idea?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Have a look at this as suggestion here:</p>
  
  <p><img src="https://i.stack.imgur.com/98LEs.png" alt="enter image description here"></p>
  
  <p>I mean how can I make it get back to 2 instead of 3</p>