BaseClass
Lean yet robust JavaScript inheritance.
$ bower install baseclassjs
$ npm install baseclassjs
BaseClass is a fast, lightweight, and non-intrusive inheritance library. Code in natural JavaScript but now with the power of simple inheritance.
BaseClass is provided as a CommonJS module, as well as a global function.
You can pick whichever version you prefer. The global function lives
in dist/baseclass.min.js
, and the CommonJS module can be require
'd
after it is npm install
'd.
Every release, performance data is gathered and graphed to ensure every version of the library is blazing fast.
BaseClass() & extend()
Here's a quick example showing a typical class setup.
// pet.jsvar BaseClass = ;module { // Declare your root class with the BaseClass constructor. return ;};
// dog.jsvar Pet = ;module { return ;};
// my-app.jsvar Dog = woofie = ;woofie; // --> Hi there! I'm Woofie and I'm a grey dog.
This inheritance chain can continue on as deep as you want it to be. To
reach deeper into the chain, just use the .base
notation. For example
if you want data from two levels deep, that would look
like child.base.base.data
.
Since all properties are brought over to each child, children will always
have access to an extend
method to create more children.
_create method
All levels of the inheritance chain are assigned a _create
method. This
method is stubbed when not explicitly defined. The _create
method is
a callback that executes after the root parent is created and again after
each child is extended. The this
object is bound to the newly setup
instance. Here is an example of populating a custom data-structure after
it has been newly created.
// Where MyCollection provides an add() method.module { return ;};
this.base
Any child can access its parent's methods with the this.base
object.
This is provided automatically to each child method and works in the
same way as Java's super
keyword.
{ return ;}
{ return ;}
{ return ;}
// my-app.jsvar mycar = ;console; // --> alert: beep beep, ahooooga
BaseClass.Abstract
If you want your base class to enforce an override, you can use the
Abstract
method provided from the BaseClass function. Simply drop it into
place like this:
{ return ;}
{ return ;}
// my-app.jsvar mycar = ;mycar; // --> Throws JS Error!
Calling an abstract method that has not been overridden will result in a JS Error being thrown.
BaseClass.Stub
Sometimes you only want to reserve an attribute name to ensure that it's
provided to all children. This can be done easily with the Stub
method.
// vehicle.jsvar { return ;};
// car.jsvar { return ;};
// my-app.jsvar whip = ;whip; // --> Nothing happens.
- See: http://cobbdb.github.io/baseclass
- See: http://github.com/cobbdb/baseclass
- License: MIT