title: JS: Confusion about inheritance
body: <p>I am familiar with OOP concepts through the languages like C++, Java. Right now I am trying to learn JavaScript as a hobby, mainly due to the interest in WebGL. But I am having troubles with prototype based inheritance.</p>
  
  <p>Let's say I have a base class which accepts a parameter in constructor. And I need to extend this. The way I am doing this is shown below.</p>
  
  <pre><code>function Base(n) {
      this._n = n;
  }
  
  Base.prototype.print = function() {
      console.log(this._n);
  }
  
  function Derived(n) {
      Base.call(this, n);
  }
  
  Derived.prototype = new Base;
  Derived.prototype.constructor = Derived;
  </code></pre>
  
  <p>Now this is what I understand: A single <code>Base</code> object is serving as the prototype of <code>Derived</code>. So all instances of <code>Derived</code> will inherit properties from this <code>Base</code> object, e.g. the <code>print</code> method. When I call <code>new Derived(10)</code> then a new object is created, function <code>Derived</code> is called in the context of this newly created object, i.e. <code>this</code> points to newly created object, and function <code>Base</code> is called from function <code>Derived</code>, and then <code>_n</code> is created and assigned the value 10. So if I create 5 <code>Derived</code> objects, all of them will have their own <code>_n</code> property. So far this is okay.</p>
  
  <p>But I don't like this line:</p>
  
  <pre><code>Derived.prototype = new Base;
  </code></pre>
  
  <p>Function <code>Base</code> expects an argument but I am passing nothing here. There is no point of passing a parameter here as this object will act as prototype of <code>Derived</code>. And I don't need any value of <code>_n</code> for this prototype object. But what if the function <code>Base</code> depends on the argument? Say, <code>Base</code> loads a resource and the path is passed as parameter. What to do then? </p>
  
  <p>To summarize, my questions are:</p>
  
  <ol>
  <li>What to do with data members in prototype object (<code>_n</code> in this example)? </li>
  <li><code>Derived.prototype = new Base;</code> is creating an instance of <code>Base</code> and this will remain in memory always (assuming <code>Derived</code> is defined in global space). What to do if <code>Base</code> class is very costly and I don't want an extra object? </li>
  </ol>