title: Loop through each new Object from Constructor
body: <p>Firstly, sorry for my lack of terminology.</p>
  
  <p>If I have a constructor</p>
  
  <pre><code>function myObject(name, value){
      this.name = name;
      this.value = value;
  }
  </code></pre>
  
  <p>and I make a few objects from it</p>
  
  <pre><code>var One = new myObject("One", 1);
  var Two = new myObject("Two", 2);
  </code></pre>
  
  <p>Can I loop through each new Object made from the <code>myObject</code> class, without putting each new Object into an Array?</p>
  
  <hr/>
  
  <p>would it be possible to add an Instantly Invoking Function to the constructor that adds the Object into an array?</p>
  
  <p>e.g.</p>
  
  <pre><code>function myObject(name, value){
      this.name = name;
      this.value = value;
  
      this.addToArray = function(){
          theArray.push(this);        // this is the IIFE
      }(); 
  }
  </code></pre>
  
  <p>that way any new objects created instantly run this function and get added to the array.</p>
  
  <p>Is this possible? ( current syntax does not work, obviously )</p>
  
  <hr>
  
  <p><em>EDIT</em> Coming back to this one year later I can tell you that it IS possible. You just call the function inside the constructor like so:</p>
  
  <pre><code>function myObject(name, value){
      this.name = name;
      this.value = value;
  
      this.addToArray = function(){
          theArray.push(this);
      };
  
      this.addToArray();
  
  }
  </code></pre>
  
  <p>Here is an example of this in JSFIDDLE, pushing each object into an array on instantiation and then calling each object's <code>.speak()</code> method directly from the array.</p>
  
  <p><a href="https://jsfiddle.net/Panomosh/8bpmrso1/" rel="nofollow noreferrer">https://jsfiddle.net/Panomosh/8bpmrso1/</a></p>