title: Why is my effort to implement Parasitic Inheritance in Javascript failing here?
body: <p>I am playing with classical inheritance in Javascript (in Chrome) following the example of <a href="http://www.crockford.com/javascript/inheritance.html" rel="nofollow">Crockford</a>. I like the parasitic interface but am looking for a bit cleaner way to encapsulate inheritance.</p>
  
  <p>There are a couple additional requirements I am trying to meet:</p>
  
  <ol>
  <li>I'd like to be able to override methods of the parent class with methods in the child class that can call the parent method</li>
  <li>I don't want to have to re-declare properties of the parent class in the child class.</li>
  </ol>
  
  <p>This is my parent class <code>Parasite</code> with an <code>extend</code> method that tries to encapsulate inheritance and take advantage of <code>Object.defineProperty</code>:</p>
  
  <pre><code>function Parasite(host) {
      var self = {};
      self.host = host;
      self.swollen = false;
  
      self.init = function() {
          console.debug('Parasite.init');
          return self;
      };
  
      self.suck = function() {
          console.log("I'm a parasite who sucks on " + self.host);
          self.swollen = true;
          return self;
      };
  
      self.extend = function(child) {        
          for(var prop in self) {
              if (prop == 'extend') {     // skip extend
                  console.debug('skip extend');
                  continue;
              }
  
              var is_extended = child.hasOwnProperty(prop);
              var is_accessor = typeof self[prop] != "function";
  
              // inherit prop
              if (! is_extended) {
                  child[prop] = self[prop];
                  console.debug('prop', prop, 'inherited by child');
              }
              // default: override
              else {
                  console.debug('prop', prop, 'overridden by child');
              }
  
              // For accessors, parent should reference child. This tries to
              // synchronize them on the child's accesor.
              if (is_accessor) {
                  var accessor = prop.toString();
                  console.warn('define accessor for', accessor, ':', child[accessor]);
                  Object.defineProperty(self, accessor, {                      
                      get: function() {
                          var val = child[accessor];
                          console.debug('getting', accessor, val, 'from', child, 'for', self);
                          return val;
                      },
                      set: function(val) { 
                          console.debug('setting', accessor, val, 'from', child, 'for', self);
                          child[accessor] = val;
                      },
                      enumerable: true,
                      configurable: true
                  });
              };
          }
  
          child.parent = self;
          return child;
      };
  
      self = self.init();
      return self;
  }
  </code></pre>
  
  <p>This is my child class, <code>Tick</code>:</p>
  
  <pre><code>function Tick(host) {
      var self = {};
  
      self.suck = function() {
          self.parent.suck.call();
          self.engorge();
      };
  
      self.engorge = function() {
          console.log("And now I'm engorged with blood.");
      };
  
      self.init = function() {
          var parent = new Parasite(host);
          self = parent.extend(self);
          return self;
      };
  
      self = self.init();
      return self;
  }
  </code></pre>
  
  <p>You can find my latest fiddle here (warning: infinitely recurses in Firefox, but not in Chrome):</p>
  
  <p><a href="http://jsfiddle.net/SSDgv/23/" rel="nofollow">http://jsfiddle.net/SSDgv/23/</a></p>
  
  <p>The console output illustrates where the accessor issues are occurring.</p>