title: Javascript inheritence extend method tips
body: <p>I am working on an extend method to create subtypes of objects and functions in JavaScript. I am struggling a little understanding exactly how these two lines are different:</p>
  
  <ul>
  <li>f.prototype = p;</li>
  <li>f.prototype = new p();</li>
  </ul>
  
  <p>Could I get an explanation of what is actually taking place behind the scenes?</p>
  
  <p>My understanding is when I pass in an object, if I use 'new p();' the constructor is called which may not exist.  Is this correct?  Would there be a way to consolidate any of this code?</p>
  
  <pre><code>    function extend(p) {
          if (p == null) throw TypeError(); 
  
          var f = function() {}, 
              t = typeof p;
  
          if( t === 'object' ) {
              f.prototype = p;
  
              f.prototype.constructor = f;
              return new f();
  
          } else if( t === 'function' ) {
              f.prototype = new p(); 
  
              f.prototype.constructor = f;
              return f; 
          } else {
              throw TypeError();
          }
      }       
  
      // ==========================
      var Person = {
          firstName: 'John',
          lastName: 'Doe'
      };
      console.log( 'Person: ', Person );
  
      var Employee = extend( Person );
      Employee.prototype = {
          employeeID: 0
      }
  
      console.log( 'Employee: ', Employee );
  
      var Manager = extend( Employee );
      Manager.prototype.fireEveryone = function() {
          alert( 'Everyone is fired!!!!' );
      }
      console.log( 'Manager: ', Manager );
  </code></pre>
  
  <p>Thanks</p>