title: Composition, Inheritance, and Aggregation in JavaScript
body: <p>There is a lot of information about composition vs inheritance online, but I haven't found decent examples with JavaScript. Using the below code to demonstrate inheritance:</p>
  
  <pre><code>function Stock( /* object with stock names and prices */ ) {
      for (var company_name in arguments[0]) {
          // copy the passed object into the new object created by the constructor
          this[company_name] = arguments[0][company_name]; 
      }
  }
  
  // example methods in prototype, their implementation is probably redundant for
  // this question, but list() returns an array with toString() invoked; total()
  // adds up the stock prices and returns them. Using ES5 feature to make
  // inherited properties non-enumerable 
  
  Stock.prototype =  {
      list: function () {
          var company_list = [];
          for (var company_name in this)
              company_list.push(company_name);
          return company_list.toString();
      },
      total: function () {
          var price_total = 0;
          for (var company_name in this)
              price_total += this[company_name];
          return '$' + price_total;
      }
  };
  
  Object.defineProperties(Stock.prototype, {
      list: { enumerable: false },
      total: { enumerable:false }
  });
  
  var portfolio = new Stock({ MSFT: 25.96, YHOO: 16.13, AMZN: 173.10 });
  portfolio.list();  // MSFT,YHOO,AMZN
  portfolio.total(); // $215.19
  </code></pre>
  
  <p>(To make the code smaller, you can leave out the method implementations, like: <code>Stock.total = function(){ /* code */ }</code> I just put them in there to be fancy). If composition is favored for a lot of situations in OOP, how come most people using JavaScript seem to only use prototypes and inheritance? I did not find a lot of information about composition in JavaScript online, only in other languages.</p>
  
  <p>Can someone give me an example using the above code to demonstrate composition and aggregation? </p>