classy-classical

Classical inheritance in two JavaScript functions, without sacrificing personal preference

Usage no npm install needed!

<script type="module">
  import classyClassical from 'https://cdn.skypack.dev/classy-classical';
</script>

README

classy-classical

Classical inheritance in three JavaScript functions, without sacrificing personal preference.

Installation

npm install classy-classical

Functions

Only three functions are needed for inheritance.

classyClassical.extending(class, superClass)

Rename of util.inherits, used for extending classes. In the following example, RedCar will have the drive function and would return the color 'red'.

var classyClassical = require('classy-classical');

function Car (preOwned)
{
    this.preOwned = preOwned;
}

Car.prototype.drive = function ()
{
    return 'Broom broom!';
}

Car.prototype.getColor = function ()
{
    return 'unknown';
}


function RedCar (preOwned)
{
    classyClassical.super(this, Car)(preOwned);
}

classyClassical.extending(RedCar, Car);

RedCar.prototype.getColor = function ()
{
    return 'red';
}

classyClassical.implementing(class, implementedFunctionNames)

The implementing function is used for checking that functions have been implemented. In the following example, an Error will be thrown as the function name 'isAlive' has not been implemented! On the other hand, no error would be thrown if the function had been implemented.

var classyClassical = require('classy-classical');
var animal = [ 'isAlive', 'walk' ];

function Cat () {};

Cat.prototype.walk = function ()
{
    console.log('doo bee doo bee doo');
}

classyClassical.implementing(Cat, animal);

classyClassical.super(instance, superClass[, superFunctionName])

(superFunctionName may be ommitted if the super constructor is to be called)

The super function can be used to call a super function of a class. In the following example, 'unknown' would be returned if getColor were to be called.

var classyClassical = require('classy-classical');

function Plane (preOwned)
{
    this.preOwned = preOwned;
}

Plane.prototype.drive = function ()
{
    return 'Whoosh whoosh!';
}

Plane.prototype.getColor = function ()
{
    return 'unknown';
}


function RedPlane (preOwned)
{
    classyClassical.super(this, Plane)(preOwned);
}

classyClassical.extending(RedPlane, Plane);

RedPlane.prototype.getColor = function ()
{
    return classyClassical.super(this, Plane, 'getColor');
}