title: Javascript: Add method to all objects in array
body: <p>Imagine the following code:</p>
  
  <pre><code>$.get( "ajax/getColorData.php", function( data ) {
    this.colorData = data;
  });
  </code></pre>
  
  <p>now envision that the value of 'data' is:</p>
  
  <pre><code>this.colorData = [
      {
          colorName: 'Red',
          colorIsInRainbow:true
      },
      {
          colorName: 'Orange',
          colorIsInRainbow: true
      },
      {
          colorName: 'Magenta',
          colorIsInRainbow: false
      }
  ];
  </code></pre>
  
  <p><strong>Question 1</strong></p>
  
  <p>Now, after I download the data, let's say that I want to add a method, "colorStartsWithR" to every entry in the array. I "think" that rather than define it on every single member of the array, I could somehow define this method on the prototype. But, I'm not sure that I can do that because these objects were not created by me, but were returned by $.get, so it's not clear to me if I'm thinking in the right direction.</p>
  
  <p><strong>Question 2</strong></p>
  
  <p>And to take it a bit further, what if I wanted to add a property to every member of the array, specifically:</p>
  
  <pre><code>    {
          colorName: 'Magenta',
          colorIsInRainbow: false,
          userClickedThisColor:ko.observable()
      }
  </code></pre>
  
  <p>This way, I can bind (via knockoutjs) the list and include a checkbox. In this case, I question whether or not the prototype would come in handy because each member should get its own ko.observable property. A quick instinct is to do something like:</p>
  
  <pre><code>for (var i=0;i&lt;this.colorData.length;i++)
  {
   this.colorData[i].userClickedThisColor=ko.observable()
  }
  </code></pre>
  
  <p>and that works fine, but imagine that there is more than one way to retrieve a list of colors, or that a user can create new color instances, and that the customization I did above is more complicated. I would now need to duplicate the above "add-on" logic. Is there a smarter way?</p>
  
  <p>Having spent most of my time in strongly-typed languages, sometimes these things aren't as obvious to me. Thanks for any suggestions...</p>
  
  <p>-Ben</p>