Download the full version or the minified version here:

Version 1.4.0 - MIT licensed

MClass JavaScript OOP

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#.

Supported object oriented features:

Close to JavaScript prototypal inheritance

MClass tries to stay close to the priciples of prototypal inheritance (in contrast to some other oop libraries)

  1. The JavaScript keyword instanceof is fully supported
  2. Easy conversion from object to string with toString()
  3. Economical memory consumption: an instance object only contains instance data, no methods or other data is copied into the instance object

Because mClass provides private functions and variables, it provides better encapsulation and decoupling than classes from ECMAScript 2015 (6th edition), making mClass useful even in the newest version of JavaScript.

Possible features to add in the future: interfaces and ES5 object methods support.

The ‘m’ in mClass stands for module pattern, which is used to provide private variables and functions.

Example

var Bird = mClass({
	// inherit from another class
	extends: Animal,
	// Override public method and member
	public: {
		sound: function() {
			return "tweet";
		},
		legs: 2
	}
});
 
var bird = new Bird();

Take a look at the annotated unit tests for more examples.

CDN

The easiest way is to use a content delivery network. Just paste the following code in your HTML:

<script src="https://cdn.jsdelivr.net/mclass/1.4.0/mclass-min.js" integrity="sha384-pow1ICcgntcic3w/14Pxoa8jjIkhQpdulrQ1smUTZgrgtD8ppcl9qJqOJQKNnGFz" crossorigin="anonymous"></script>

Module

For a modular approach, mClass supports both AMD and CommonJS.

AMD

require(['mclass'], function(mClass) {
// your code here...
}

CommonJS

var mClass = require("./mclass");

Package

To install the whole package, you can use one of the following commands:

git clone https://github.com/edwinm/mClass.git
 
npm install mclass --save-dev
 
bower install mclass

Unit tests

You can run the unit tests. Let me know when you see a test fail.

Open Source

MClass is copyright 2015 Edwin Martin and released under the open source MIT license. The code is available on GitHub, where you can clone and improve it and make pull requests.

Edwin Martin <edwin@bitstorm.org>