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

mclass

v1.4.0

Published

Provides class like programming to JavaScript while staying close to prototypal inheritance.

Downloads

8

Readme

mClass

Implementing class like functionality using prototypal inheritance.

Making object oriented programming in JavaScript a bit easier, at least for people with a background in class based programming languages like C++, Java and C#.

See the mClass homepage for more information.

Examples

See tests.js for examples of how to use mClass.

/*
How to use mClass
 */

var Animal = mClass(function() {
	// Private
	var info = "Carl Linnaeus is known as the father of modern taxonomy.";

	return {
		// Constructor
		construct: function(specie) {
			this.specie = specie;
		},
		// Public methods and members
		public: {
			sound: function() {
				return undefined;
			},
			toString: function() {
				return "I'm a " + this.specie + " with " + this.legs + " legs saying " + this.sound();
			}
		},
		// Static methods and members
		static : {
			info: function() {
				return info;
			}
		}
	};
});

var snake = new Animal("snake");

test( "instanceof", function() {
	ok( snake instanceof Animal );
});

test( "constructor", function() {
	ok( snake.constructor === Animal );
});



// A class without private methods or members is much easier
var Bird = mClass({
	// inherit from another class
	extends: Animal,
	// Override public method and member
	public: {
		sound: function() {
			return "tweet";
		},
		legs: 2
	}
});

var dove = new Bird("dove");

test( "instanceof", function() {
	ok( dove instanceof Animal );
});

test( "instanceof parent", function() {
	ok( dove instanceof Bird );
});

test( "overridden method", function() {
	ok( dove.sound() == "tweet" );
});

test( "overridden member", function() {
	ok( dove.legs == 2 );
});

test( "inherited method toString", function() {
	ok( ''+dove == "I'm a dove with 2 legs saying tweet" );
});

// If you don't like new, you can use Object.create()
var sparrow = Object.create( Bird.prototype, {specie: {value: "sparrow"}} );
test( "Object.create()", function() {
	ok( ''+sparrow == "I'm a sparrow with 2 legs saying tweet" );
});

// A class with private methods and members is a little bit more work
var Fish = mClass(function() {
	function calculateLegs() {
		return 0;
	}
	var sound = "blub";

	return {
		extends: Animal,
		public: {
			sound: function() {
				return sound;
			},
			// Call a parent method with the _super member (both this._super and Fish._super are possible)
			toString: function() {
				return this._super.toString.call(this)+" and I can swim";
			},
			legs: calculateLegs()
		}
	};
});

// Test inherited static function
test( "inherited static function", function() {
	ok( Fish.info() == "Carl Linnaeus is known as the father of modern taxonomy.");
});

var minnow = new Fish("minnow");

test( "inherited function using private function", function() {
	ok( minnow.sound() == "blub" );
});

test( "toString with super", function() {
	ok( ''+minnow == "I'm a minnow with 0 legs saying blub and I can swim" );
});

// Test augmentation

// You can only extend from one class, but augment your classes with other classes and objects

// Augment a object literal
var weightService = {
	weight: 10,
	getWeight: function() {
		return this.weight;
	}
};

// Augment another mClass instance. You can even use private variables
var Skin = mClass(function() {
	var skin;

	return {
		public: {
			setSkin: function(newSkin) {
				skin = newSkin;
			},
			getSkin: function () {
				return skin;
			}
		}
	}
});

// A new class augmenting the object and class above
var Dog = mClass({
	extends: Animal,
	augments: [weightService, new Skin()],
	public: {
		sound: function() {
			return "woof";
		}
	}
});

var dog = new Dog('dog');

test( "augment with object literal", function() {
	ok( dog.getWeight() == 10 );
});

dog.setSkin('furry');
test( "augment with mClass instance", function() {
	ok( dog.getSkin() == 'furry' );
});

MClass is written by Edwin Martin and is licensed with the MIT license.