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

klassified

v4.0.1

Published

A simple object model for JavaScript

Downloads

4,827

Readme

⚠ The project is archived. No further development is planned.

Klassified Build Status

A simple object model for JavaScript.

Klassified provides a base class object that can be subclassed using its subclass class method, as in the following example:

var animal = object.subclass(function(that, my) {
    my.initialize = function(spec) {
        my.super(spec);
        my.name = spec.name;
    }
    that.getName = function() {
        return my.name;
    };
});

animal.class(function(that) {
    that.named = function(name) {
        return that({name: name});
    };
});

var dog = animal.subclass(function(that, my) {
    that.getName = function() {
        return 'dog named' + that.super();
    };
});

that represents the receiver (the instance or the class depending on the context).

Public and protected properties

Public methods are attached to the instance (that), while protected methods are attached to my.

Object creation

Instances can be created by calling a class function like the following:

var milou = dog({name: 'milou'});
milou.getName(); // => 'milou

Initialization

The spec literal object is passed to my.initialize, which is called upon instance creation.

my.super should almost always be called from within initialize.

Super calls

Klassified has support for super calls using either that.super for public methods or my.super for protected methods.

Default methods in object

object provides the following instance methods and properties:

  • that.klass returns the class of the instance
  • my.subclassResponsibility method throwing an exception because a method should have been overridden.
  • my.initialize initialization method, takes a literal spec object.

object also provides the following class-side methods and properties:

  • object.subclasses returns an array of the direct subclasess of a class.
  • object.allSubclasses method that returns all the subclasess of a class.
  • object.subclass method used to create a new subclass of a class.

Abstract classes

Klassified supports abstract classes using abstractSubclass:

var animal = object.abstractSubclass(function(that, my) {

    // [...]

});

var dog = animal.subclass(function(that, my) {

    // [...]

});

animal(); // => Error: Cannot instantiate an abstract class
dog(); // => New dog instance

Singleton classes

Klassified support singleton classes using singletonSubclass:

var service = object.singletonSubclass(function(that, my) {

    // [...]

});

service.instance(); // Return the single instance of the class
service(); // => Error: Cannot create new instances of a singleton class, use `instance` instead.

Getters and setters generation

Klassified can generate getters and setters for protected properties on my, as below:

var animal = object.subclass(function(that, my) {
	my.initialize = function(spec) {
		my.name = spec.name;
	};

	my.get('name');
    my.set('name');
});

var a = animal();
a.setName('milou');
a.getName(); // => 'milou'

Custom getters and setters are also supported:

var dog = animal.subclass(function(that, my) {
	my.get('name', function() {
        return 'A dog named ' + my.name;
    });
    my.set('name', function(value) {
        my.name = value.toUpperCase();
    });
});

var a = animal();
a.setName('milou');
a.getName(); // => 'A dog named MILOU'