Inheritance

Often you need classes with similar properties and methods to exist in another existing class. In fact, it is considered good practice to define a generic class which contains reusable functionality and can adapt for specific needs.

To make this possible, classes can be extensions of other classes. The extended or derived class has all properties and methods of the base class. This is called inheritance, despite the fact that nobody died in the process.

Classes are extended using the parameter extends.

var NamedCart = Class({ extends: Cart }, {
    'public string owner' : null
});

This creates a class definition NamedCart that has all properties and methods of Cart plus the additional owner property. You can still use the generic functionality of Cart on an object instantiated from NamedCart.

var ncart = new NamedCart();

ncart.setOwner('Bob');         // Name the cart
console.log(ncart.getOwner()); // Print "Bob" to the console
ncart.addItem("Some Item", 1); // (inherited functionality from Cart)

This is also called a parent-child relationship. You create a class, parent, and use extends to create a new class based on the parent class: the child class. You can even use this new child class and create another class based on this child class. This process is also known as 'inheritance chain'.

Classes must be defined before they are used. If you want the class NamedCart to extend on Cart, you have to make sure Cart is already defined. If you want a class named BonusCart to extend on NamedCart, then NamedCart must be defined first.

Overriding

Sometimes it is necessary to override the functionality of a parent method. This can be achieved by simply redeclaring the same method or property in the derived class.

Beware that the visibility of a property or method can't be changed. For example, if a property is public in the parent class and you try to declare it private in the child class, an exception is thrown.

When overriding functions, you must specify the same type parameters as the parent, or it will create an overload instead.

var NamedCart = Class({ extends: Cart }, {
    // Override addItem from Cart
    'public addItem(string, number)' : function (name, count) {
        // Call the parent method
        this.super('addItem', name, count);

        // Print something to the console
        console.log(this.getOwner(), 'added item', name, 'to the cart!');
    }
});

The super method is used to call an overridden method from the parent class. The object context of the parent class (the this variable) remains a reference to the child class.

Warning

extends is an existing ECMAScript keyword. Some browsers, notably Internet Explorer 8 and below will throw an error when these keywords are used. You'll need to wrap the keyword in quotes to work around this. The same applies to implements and super.

var ECMA5CompatibleCart = Class({ 'extends' : Cart }, {

    'public addItem' : function (name, count) {
        // Use array-syntax to call the "super" method.
        this['super']('addItem', name, count);
    }

});