title: Class Inheritance in Knockout with method overwrite
body: <p>I'm trying to do class Inheritance in Knockout to apply the DRY principle but haven't been 
  very successful.</p>
  
  <p>What I'm trying to achieve are 2 types of servers that share some similarities without repeating code or a lot of code. </p>
  
  <p>This is my latest try with some functionalities not working has expected.</p>
  
  <p>Parent Class:</p>
  
  <pre><code>function BaseServer(options, builder) {
  
    this.formatted_price = ko.computed(function() {
      return utils.format_price(this.price(), options.prices.currency());
    }, this);
  
    this.drives = ko.observableArray([]);
  
    this.number_of_instances = ko.observable(1);
  
    // Not sure if this is a good approach for this problem
    for (i = 0; i &lt; options.ssd.length; i++) {
      self.drives.push(new builder.ssd(options.ssd[i], options.prices));
    }
    for (i = 0; i &lt; options.hdd.length; i++) {
      self.drives.push(new builder.hdd(options.hdd[i], options.prices));
    }
  
    // Will not work there is not this.cpu nor this.ram
    this.price = ko.computed(function() {
      var total = 0
      total += this.cpu.price();
      total += this.ram.price();
      total += _.reduce(this.drives(), function(mem, drive) {
        return drive.price();
      }, 0);
      return total;
    }, this);
  }
  </code></pre>
  
  <p>Things I think are wrong:</p>
  
  <ul>
  <li>Price function does not work because the parent has no access to ram, cpu variables.</li>
  <li>The builder method seems to me a really odd way of achieving this result but it works.</li>
  <li>I'm passing functions in the argument options that are then called by the class.</li>
  </ul>
  
  <p>Then the children.</p>
  
  <pre><code>function ServerGama(options){
    // Constructors for disks
    var builder = {
          ssd: SsdGama,
          hdd: HddGama
        },
        self = this;
    ko.utils.extend(self, new BaseServer(options, builder));
  
    // Normal attributes
    self.cpu = new CpuGama(options.cpu.up, options.cpu.down, options.prices);
    self.ram = new RamGama(options.ram.up, options.ram.down, options.prices);
  }
  
  function ServerBeta(options){
    var builder = {
          ssd: SsdBeta,
          hdd: HddBeta
        },
        self = this;
    ko.utils.extend(self, new BaseServer(options, builder));
  
    // Normal attributes
    self.cpu = new CpuBeta(options.cpu, options.prices);
    self.ram = new RamBeta(options.ram, options.prices);
    self.licenses = new server_licenses([
      {
        'name': 'Server 2008',
        'price': options.prices.cost_per_2008
      },
      {
        'name': 'Server 2009',
        'price': options.prices.cost_per_2009
      }
    ], options.prices.currency, options.choice);
    // This price does not seem to overwrite BaseServer price
    this.price = ko.computed(function() {
      // This will not work because we are losing the biding that price is making to the cpu, ram, disk variables
      var total = this.price.call(this);
      total += self.licenses.price();
      return total;
      }, 0);
    }, this);
  }
  </code></pre>
  
  <p>Things I think are wrong:</p>
  
  <ul>
  <li>The formatted_price from parent wont use this overwrite from the BetaServer class.</li>
  <li>Maybe I could construct the disks in another way</li>
  </ul>