JavaScript - Object - Classical Inheritance

This helper provides classical inheritance on objects. This way a sub class can inherit public members from a super class. Private members will not (can not) be inherited.

Usage:


    // The super class
    var Foo = function(name){
        this.name = name;
    };
    Foo.prototype.getName = function(){
        return this.name;
    };


    // The sub class
    var Bar = function(name,tracks){
        // Calls the superclass constructor in the scope of "this"
        Bar.superclass.constructor.call(this,name);
        this.tracks = tracks;
    };
    // Call the extend function telling it that Bar is a sub of Foo
    inheritence.extend(Bar,Foo);
    Bar.prototype.getTracks = function(){
        return this.tracks;
    };


    // Instantiat "a" from "Foo"
    var a = new Foo('Gluecifer');
    a.getName();

    // Instantiat "b" from "Bar"
    var b = new Bar('Hellacopters',14);
    b.getTracks();
    b.getName();

Example:

Code for this example here.

Acknowledgment:

This page is a result of techniques described by Ross Harmes and Dustin Diaz

Page by: Trygve Lie