npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

baseclassjs

v4.0.1

Published

Lean yet robust JavaScript inheritance.

Downloads

27

Readme

BaseClass Bower version NPM version

Lean yet robust JavaScript inheritance.

$ bower install baseclassjs
$ npm install baseclassjs

NPM infoNPM downloads


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.js
var BaseClass = require('baseclassjs');
module.exports = function (name) {
    // Declare your root class with the BaseClass constructor.
    return BaseClass({
        name: name,
        color: 'grey',
        speak: function () {
            console.log('Hi there!');
        }
    });
};
// dog.js
var Pet = require('./pet.js');
module.exports = function (name) {
    return Pet(name).extend({
        // The 'this.base' object is used to access a parent's methods.
        speak: function () {
            this.base.speak();
            console.log("I'm " + name + " and I'm a "  + this.color + ' dog.');
        }
    });
};
// my-app.js
var Dog = require('./dog.js'),
    woofie = Dog('Woofie');
woofie.speak(); // --> 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.exports = function () {
    return MyCollection().extend({
        _create: function () {
            this.add([1, 2, 3, 4]);
        }
    });
};

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.

function Machine() {
    return BaseClass({
        alert: function () {
            return 'alert: ';
        }
    });
}
function Vehicle() {
    return Machine().extend({
        honk: function () {
            return this.base.alert() + 'beep beep';
        }
    });
}
function Car() {
    return Vehicle().extend({
        honk: function () {
            return this.base.honk() + ', ahooooga';
        }
    });
}
// my-app.js
var mycar = Car();
console.log(mycar.honk()); // --> 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:

function Vehicle(model) {
    return BaseClass({
        model: model,
        // Drop it in like any other property.
        drive: BaseClass.Abstract
    });
}
function Car(model) {
    return Vehicle(model).extend({
        color: 'blue'
        // Notice we did -not- override the drive() method.
    });
}
// my-app.js
var mycar = Car('Honda');
mycar.drive(); // --> 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.js
var Vehicle = function (model) {
    return BaseClass({
        model: model,
        // Drop it in like any other property.
        honk: BaseClass.Stub
    });
};
// car.js
var Car = function (model) {
    return Vehicle(model).extend({
        weight: '1000lbs'
        // Notice we did -not- override the honk() method.
    });
};
// my-app.js
var whip = Car('Honda');
whip.honk(); // --> Nothing happens.

  • See: http://cobbdb.github.io/baseclass
  • See: http://github.com/cobbdb/baseclass
  • License: MIT