descend

Function to set up an inherited class that calls its ancestor constructors automatically. Can also be attached to constructors.

Descend.js

![NPM version][npm-badge] ![Build status][travis-badge] [npm-badge]: https://badge.fury.io/js/descend.png [travis-badge]: https://travis-ci.org/moll/js-descend.png?branch=master

Descend.js gives you a simple descend function you can attach to constructors or call directly to give you an inherited class (constructor with inherited prototype) that calls its ancestor constructors automatically when instantiated.

It also instantiates if any of the descendant constructors are called without using the new keyword.

Descend.js also sets the constructor property correctly on descendants. It does not set any other property thereby keeping your objects clean.

npm install descend

Attach descend (or name it inherit or whatever you like) on a constructor:

function Model() { console.log("Model initialized.") }
Model.descend = require("descend")
 
var Child = Model.descend(function() { console.log("Child initialized.") })
new Child // Will output both "Model initialized." and "Child initialized." 

If you want descendant classes to also have the descend function, you might want to add it to them afterwards:

var descend = require("descend")
function Model() {}
 
Model.descend = function() {
  var descendant = descend.apply(this, arguments)
  descendant.descend = this.descend
  return descendant
}

Call descend with the constructor and the descendant's initializer:

var descend = require("descend")
function Model() { console.log("Model initialized.") }
var Child = descend(Model, function() { console.log("Child initialized.") })
new Child // Will output both "Model initialized." and "Child initialized." 

Descend.js is released under a Lesser GNU Affero General Public License, which in summary means:

  • You can use this program for no cost.
  • You can use this program for both personal and commercial reasons.
  • You do not have to share your own program's code which uses this program.
  • You have to share modifications (e.g bug-fixes) you've made to this program.

For more convoluted language, see the LICENSE file.

Andri Möll typed this and the code.
Monday Calendar supported the engineering work.

If you find Descend.js needs improving, please don't hesitate to type to me now at andri@dot.ee or create an issue online.