chaining extend

by Artem

JavaScript

'use strict';

function extend(Child, Parent) {
  var F = function() {}
  F.prototype = Parent.prototype
  Child.prototype = new F()
  Child.prototype.constructor = Child
  Child.superclass = Parent.prototype
}

function __extend(C, P) {
  return (function(superF) {
    var proto = {};

    for (var p in C.prototype) {
      if (C.prototype.hasOwnProperty(p)) {
        proto[p] = C.prototype[p];
      }
    }

    extend(C, superF);

    for (var p in proto) {
      C.prototype[p] = proto[p];
    }

    return C;

  }(P));
}

function GrandParent() {}
GrandParent.prototype.grandParentFunc = function() {};

function Parent() {}
Parent.prototype.parentFunc = function() {};
__extend(Parent, GrandParent);

function Child() {}
Child.prototype.childFunc = function() {};
__extend(Child, Parent);

var c = new Child();
console.dir(c);