Parasite Combination Inheritance

Nicholas C. Zakas

by Rahul Shetty

JavaScript

function object(o){
      function F(){}
      F.prototype = o;
      return new F();
    }
    
    function inheritPrototype(subType, superType){
      var prototype = object(superType.prototype);
      console.log(prototype);
      prototype.constructor = subType;
      subType.prototype = prototype;
    }
    
    function SuperType(name){
      this.name = name;
      this.colors = [1, 2, 3];
    }
    
    SuperType.prototype.sayName = function(){
      alert(this.name);
    };
    
    function SubType(name, age){
      SuperType.call(this, name);
      this.age = age;
    }
    
    inheritPrototype(SubType, SuperType);
    
    
    SubType.prototype.sayAge = function(){
      alert(this.age);
    };
    
    var instance = new SubType('Rahul', 25);
    console.log(instance);