some inheritance

by steveukx

JavaScript

function extend(base, supe) {
    function F() {}
    F.prototype = supe.prototype;
    base.prototype = new F;
    base.constructor = base;
}

function AAA() {
    console.log("Constructing AAA");
}
AAA.prototype.fun = function() {};

function BBB() {
    console.log("Constructing BBB");
    AAA.apply(this, arguments);
}
extend(BBB, AAA);

BBB.prototype.func = function() {}
BBB.prototype.fun = function() {
    AAA.prototype.fun.apply(this, arguments);
};

function CCC() {
    console.log("Constructing CCC");
    BBB.apply(this, arguments);
}
extend(CCC, BBB);


var c = new CCC;

// instanceof works
console.log(c instanceof CCC);
console.log(c instanceof BBB);
console.log(c instanceof AAA);

console.log(c.fun === BBB.prototype.fun); // no new instances here

console.log(c);