How can I count the instances of an object?
<p>If i have Javascript object defined as:  </p>

<pre><code>function MyObj(){};

MyObj.prototype.showAlert = function(){
   alert("This is an alert");
   return;
};
</code></pre>

<p>Now one can call it as:</p>

<pre><code>var a = new MyObj();
a.showAlert();
</code></pre>

<p>So for 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 get a hold of number of instances of this 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 then this idea?</p>

<p><strong>EDIT:</strong></p>

<p>Have a look at this as suggestion here:</p>

<p><img src="http://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>
