title: Javascript inheritance idea (part 2)
body: <p>Okay, my first attempt at trying to explain what I was doing failed miserably.  I'm basically copying Crockford's Object.create(), except with private variables.</p>
  
  <p>If you look at the accepted answer here <a href="https://stackoverflow.com/questions/2107556/how-to-inherit-from-a-class-in-javascript">How to inherit from a class in javascript?</a>, you will see Object.create as the last pattern, which I think better fits the prototypal nature of Javascript (objects beget objects) instead of emulating classical inheritance (classes beget objects).</p>
  
  <p>If you look at Wikipedia's article on prototype based programming (<a href="http://en.wikipedia.org/wiki/Prototype-based_programming" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Prototype-based_programming</a>), you can see more of what I mean.</p>
  
  <p>The drawback with Object.create() though is that there is no support for private members.  This is what I propose:</p>
  
  <pre><code>Function.prototype.from = function(obj) {
      function F() {this.parent = Object(obj);}
      F.prototype = obj;
      var out = new F();
      this.apply(out);
      return out;
  };
  </code></pre>
  
  <p>Then, you create objects as thus:</p>
  
  <pre><code>// Create an object
  var a = function() {
      var private_property = 'blue';
      this.public_property = 7;
  
      this.public_method = function() {
          alert(this.public_property + ' ' + private_property);
      }
  }.from(null); // .from() works too, but .from(null) is more revealing
  
  
  // Create a new object using 'a' as the prototype
  var b = function() {
      var private_property = 'red';
      this.public_property = 8;
  }.from(a);
  
  
  // See the results
  a.public_method(); // Alerts '7 blue'
  b.public_method(); // Alerts '8 blue' - Parent methods use parent private variables
  
  a.public_method = function() { alert('rabbit'); };
  
  a.public_method(); // Alerts 'rabbit'
  b.public_method(); // Alerts 'rabbit'
  
  b.public_method = function() { alert('dog'); };
  
  a.public_method(); // Alerts 'rabbit'
  b.public_method(); // Alerts 'dog' - Parent method is overwritten
  </code></pre>
  
  <p>The way I made the "from" function is such that when a parent object changes its methods, if you want to prevent the change in a child instance, you can specify:</p>
  
  <pre><code>this.public_method = this.parent.public_method;
  </code></pre>
  
  <p>in the child instance.</p>
  
  <p><strike>Note also that objects created ex nihilo do not inherit from Object (hasOwnProperty, etc..).  You must explicitly specify this as .from(Object).</strike></p>
  
  <p>Benefits of this pattern:</p>
  
  <ol>
  <li>Memory is not wasted for each new instance</li>
  <li>It adheres to a <em>true</em> prototypal inheritance pattern</li>
  <li>You have access to the parent object using this.parent (this.__proto__ is browser specific)</li>
  <li>Private variables now exist</li>
  </ol>
  
  <p>There is one major drawback of this method that I can think of: the 'function()' syntax may confuse people into thinking a function is assigned to the variable instead of an object.</p>
  
  <p>My question is, are there other drawbacks that I am missing?  (Don't include drawbacks of the prototypal pattern--that's subjective--but only of my implementation).</p>