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

classes-js

v0.3.6

Published

JS class pattern; supports multi-inheritance, static & instance methods, public/protected access...

Downloads

9

Readme

Overview

classes.js provides a more traditional object oriented programming pattern than JavaScript's native prototype chaining.

In many cases, this is overkill and a more procedural or functional approach is actually more appropriate in JavaScript. However, in some cases it is really useful to have traditional OO features.

Features

  • Multiple inheritance
  • Namespaces
  • Static (class-level) methods vs. instance methods
  • Public, protected, and private levels of access
  • Works in browser, or in node
  • Only 5.5K (~1.8K minified)

Requires

Only dependency is atom.js, which is itself small and is included as a submodule.

Unit Tests

To run from command line using node.js:

npm install atom-js
node test.js      // brief
node test.js -v   // verbose

To run in a browser, open test.html.

Basic Example

When defining a class, attach static methods to thisClass, and instance methods to thisInstance. Methods are protected by default, meaning they are available to subclasses.

	classes.define('myclass', ['base'], function (thisClass, protoClass) {

		thisClass.staticMethod = function () {
			// Do something that doesn't require access to instance data.
			// Optionally call protoClass.<method> to access methods of the base
			// class(es).
			return 'My Class is aw3s0m3!!1!';
		};

		thisClass.instance = function (thisInstance, protoInstance, expose) {

			// Private methods and data are just defined inside the instance
			// closure.
			var foo = 'bar';
			function privateMethod() {
				return foo + ' baz';
			}

			thisInstance.protectedMethod = function () {
				// Instance methods, since they are defined inside both the class
				// closure and the instance closure, have access to all private,
				// protected and public methods of both.
				return thisClass.staticMethod() + ' ' + privateMethod();
			};

			thisInstance.publicMethod = function () {
				// This function won't actually be exposed as part of the instance's
				// public interface until expose() is called.
				return 'Here you go: ' + thisInstance.protectedMethod();
			};

			expose('publicMethod');
		};

	});

Once a class is defined, invoke it like this:

	var instance = classes.instantiate('myclass');

	console.log(instance.publicMethod());

Output:

"Here you go: My Class is aw3s0m3!!1! bar baz"

It is also possible to wait for a class to be defined:

	classes.once('myclass', function () {
		var instance = classes.instantiate('myclass');
	});

Namespaces

By default, classes are defined in a global namespace. However, you can easily create a new namespace if you want to ensure against class name collisions:

	var myClassNamespace = classes.namespace();

	myClassNamespace.define('myclass', [], function (thisClass) {
		// ...
	});

Multiple Inheritance

Multiple inheritance works by specifying an array of "superclasses" as the second argument to .define(), like so:

	classes.define(
		'myclass2',
		['superclass1', 'superclass2'],
		function (thisClass, protoClass) {
			// protoClass is now inherits all the protected and public members of
			// both superclass1 and superclass2.  In the case of conflicts, the
			// superclass specified *last* in the array wins.

			// ...
		}
	);

Inheriting Properties (Don't use primitives!)

It is possible to attach properties (as opposed to functions) to a class or instance. However, primitive properties (simple string, number or boolean values) are not recommended, because they will not stay in sync between class and superclass. Instead, use accessor functions, or set properties as on object or array that may have mutable members.