Module: Inheritance

Inheritance

This module introduce a "classic" inheritance mechanism as well as an helper to copy methods from one class to another. See the Inheritance method documentation below for details.

Method Summary

_callSuperConstructor
This method is attached to the prototype of each extended class as _callSuperConstructor to make it possible to call the super constructor.
_callSuperMethod
This method is attached to the prototype of each extended class as _callSuperMethod to make it possible to call super methods.
Inheritance
This method extends a class with the methods of another class preserving super methods and super constructor.

Method Detail

(static) _callSuperConstructor(ownerClass, paramsopt)

This method is attached to the prototype of each extended class as _callSuperConstructor to make it possible to call the super constructor.
Note that it is not actually visible in this module.
Parameters:
Name Type Argument Description
ownerClass function the class that calls this method.
params Array <optional>
array of parameters to be used to call the super constructor.

(static) _callSuperMethod(ownerClass, toCall, paramsopt)

This method is attached to the prototype of each extended class as _callSuperMethod to make it possible to call super methods.
Note that it is not actually visible in this module.
Parameters:
Name Type Argument Description
ownerClass function the class that calls this method.
toCall String the name of the super function to be called.
params Array <optional>
array of parameters to be used to call the super method.

(static) Inheritance(subClass, superClass, lightExtensionopt, checkAliasesopt)

This method extends a class with the methods of another class preserving super methods and super constructor. This method should be called on a class only after its prototype is already filled, otherwise super methods may not work as expected.
The _super_, _callSuperMethod and _callSuperConstructor names are reserved: extending and extended classes' prototypes must not define properties with such names.
Once extended it is possible to call the super constructor calling the _callSuperConstructor method and the super methods calling the _callSuperMethod method
Note that this function is the module itself (see the example)
Parameters:
Name Type Argument Description
subClass function the class that will extend the superClass
superClass function the class to be extended
lightExtension boolean <optional>
if true constructor and colliding methods of the super class are not ported on the subclass hence only non-colliding methods will be copied on the subclass (this kind of extension is also known as mixin)
checkAliases boolean <optional>
if true aliases of colliding methods will be searched on the super class prototype and, if found, the same alias will be created on the subclass. This is especially useful when extending a class that was minified using the Google Closure Compiler. Note however that collisions can still occur, between a property and a method and between methods when the subclass is minified too. The only way to prevent collisions is to minify super and sub classes together.
Throws:
if checkAliases is true and an alias of the super class collides with a different method on the subclass.
Type
IllegalStateException
Example
require(["Inheritance"],function(Inheritance) {
  function Class1() {
  }

  Class1.prototype = {
    method1: function(a) {
      return a+1;
    }
  };

  function Class2() {
    this._callSuperConstructor(Class2);
  }

  Class2.prototype = {
    method1: function(a,b) {
      return this._callSuperMethod(Class2,"method1",[a])+b;
    }
  };

  Inheritance(Class2,Class1);
  
  var class2Instance = new Class2();
  class2Instance.method1(1,2); //returns 4
  
});