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

classtool

v1.0.0

Published

Class like behavior composition, but better

Downloads

16

Readme

Classtool

This is a simple tool to enable class like idioms in JavaScript. It is notably different from traditional class based inheritance in the following ways:

  • inheritance structure is not part of the class definition
  • bindings to the outside are explicit (including binding to a superclass)
  • default bindings allow for easy class creation while preserving override capability

The following is an example class definition:

function ClassSpec(b) {
  var fs = b.fs || require('fs'); // example of a binding

  function Class() {
    // Body of constructor function
  };
  // Optional - Define a default (but overridable) superclass
  Class.superclass = b.superclass || require('./superClass').class();   

  Class.prototype.methodA = function() {
    // Body of method A
  };

  Class.prototype.methodB = function() {
    // Body of method B
  };

  return Class;
};
module.defineClass(ClassSpec); 

To use classtool, simply require it at the beginning of your program:

require('classtool');

There are two ways to instantiate the class in client code:

var MyClass = require('./path/to/classfile').class();
var MyClass = require('./path/to/classfile').createClass({fs: mockFileSystem});

In the first example, each call will return the same class instance (the 'default' class instance). In the second example, each call returns a new class instance. The second call also shows how you would override the default bindings (passing in an object meant to mimic the file system in this example).

Class instances can be registered in a dictionary of named classes as follows:

var MyClassForTesting = require('./classfile').createClass('testing', {fs: mockFileSystem});
var MyRealClass = require('./classfile').createClass('real', {fs: require('fs')});

You can later reference the same class instances as follows:

var MyClassForTesting = require('./classfile').class('testing');
var MyRealClass = require('./classfile').class('real');

If the name is omitted, "default" is used. The following expressions are equivalent:

var MyClass = require('./classfile').class();
var MyClass = require('./classfile').class('default');

To create an inheritance chain, use the method inherit() as follows:

MyClass.inherit(MySuperClass);
MyClass.inherit(MySuper, MySuperSuper, MySuperDeDuper);

To create instances of your class, do the following:

new MyClass();

For convenience, the "new" method is exported to allow instantiation:

var myInstance = require('./myClass').new();

This form is equivalent to:

var MyClass = require('./myClass').class();
var myInstance = new MyClass();

You may also instantiate instances of a specific class as follows:

var myTestInstance = require('./myClass').new('test');

It's also possible to explicitly invoke methods of a superclass. Invoke the superclass constructor as follows:

function Class() {
  Class.super(this, arguments);
};

Invoke any normal method in the superclass as follows:

Class.prototype.methodA = function() {
  Class.super(this, 'methodA', arguments);
};