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

clan

v0.1.0

Published

Yet another inheritance module

Downloads

2

Readme

clan

Yet another inheritance module

Install

npm install clan

Usage

Clan has a very simple API of Classical inheritance and Mixins.

Classical inheritance

For Classical inheritance just call the clan function with your object class definition -

var clan = require('clan');

var personClass = clan({
    name : 'Alice',
    sayHello: function () {
        conosle.log('Hello, I\'m ' + this.name);
        return this;
    }
});

Notice that this define our personClass but to use it we need to create Instantiate an object using the create method.

var alice = personClass.create();
alice.sayHello(); //Hello, I'm Alice

the create method calls the init method on our new object so we can rewrite personClass to have a constructor that will set our person name

var personClass = clan({
    init: function (name) {
        this._super();
        this.name = name;
        return this;
    },
    name : 'Alice',
    sayHello: function () {
        conosle.log('Hello, I\'m ' + this.name);
        return this;
    }
});

var bob = personClass.create('Bob');
bob.sayHello(); //Hello, I'm Bob

Cool but whats that _super method ? for every method we extend on our parent Class this._super will call the original method of the parent, so we can easily reuse logic. In this case we are using the base init of clan since it's doing some useful checks and cleans our object, in fact the base init first parameter is an object that override our class parameters so for changing our name we don't even need to have a custom init method, and this will also work -

var personClass = clan({
    name    : 'Alice',
    sayHello: function () {
        conosle.log('Hello, I\'m ' + this.name);
        return this;
    }
});

var bob = personClass.create({ name: 'Bob'});
bob.sayHello(); //Hello, I'm Bob

One last thing about Classes, once we have a class we can extend it using the extend method

var texanClass = personClass.extend({
    sayHello: function () {
        conosle.log('Howdy, I\'m ' + this.name);
        return this;
    }
});
var carol = texanClass.create({ name: 'Carol'});
carol.sayHello(); //Howdy, I'm Carol
console.log(carol instanceof texanClass); //true
console.log(carol instanceof personClass); //true

Notice that we can use instanceof to test if our object inherits from any class in its prototype chain.

As before we can use the _super method in child class -

var pirateClass = personClass.extend({
    sayHello: function () {
        this._super();
        conosle.log('Arrr');
        return this;
    }
});
var dave = pirateClass.create({ name: 'Dave'});
dave.sayHello(); //Howdy, I'm Dave Arrr

Mixins

To create a new mixin call the mixin method on clan function with your mixin definition -

var clan = require('clan');

var swordMix = clan.mixin({
    attack: function () {
        console.log('woosh woosh');
        return this;
    }
});

Mixins adds to an existing Object. So if we want to add sword fighting ability into dave from our last example

swordMix(dave)
console.log(dave.attack());  //woosh woosh

We extend mixns from existing mixins using the mixin method

var ambushMix = swordMix.mixin({
    hide: function () {
        console.log('');
        return this;
    }
});

So if we add ambush ability into Carol she cn now

ambushMix(carol)
carol.hide().attack();  // /*silence*/ woosh woosh

As with Classical inheritance we can use _super method to access method on the parent we extended

Bonus feature

when in development enable clan debugging immediately after requiring clan -

var clan = require('clan');
clan.debug(true);

This will give you some useful information when you inspect your code.