Clazz.js
A cross platform JavaScript library that provides a classical interface to a prototype system.
Clazz.js was built with some very simple goals in mind:
Usage
Creating a class
var Foo = Clazz.create(
{
initialize : function ()
{
},
baz : function ()
{
},
qux : function ()
{
},
static :
{
corge : function ()
{
}
}
});
var foo = new Foo();
*Note: All instance properties of your class should be defined within the constructor, anything in the definition treated as an instance member is applied to the Class prototype, meaning it will be shared by all instances of the class.
Creating a class that extends another class
var Bar = Clazz.create(
{
extend : Foo,
initialize : function ()
{
this.super();
},
baz : function ()
{
return 'bar';
},
qux : function ()
{
return 'super ' + this.super();
}
});
var bar = new Bar();
Creating a class that includes another class
Clazz.js provides a method of code reuse called includes. Behaving similarly to Ruby's mixins and PHP's traits, they enable a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.
var Baz = Clazz.Create(
{
include : [Foo]
});
var baz = new Baz();
Some things to take note with includes:
- If an include has a constructor, that constructor will be executed when the including class is being instantiated.
- The super method will not refer to the base of the class it's included in, it will refer to the hierarchy the include class may have.
- The precedence order is: an inherited member from a base class is overriden by an included member and an included member is overriden by a member from the current class.
Getting started
Node
Clazz.js is available through the Node package manager(npm), so you install like so:
npm install class
and bring into your code like so:
var Clazz = require('clazz');
Browser
To use Clazz.js in a browser envrionment it's as bringing it in using a script tag like so:
<script type="text/javascript" src="path/to/Clazz.js"></script>
To remove from the global namespace, you can use Clazz.noConflict(), like so:
Namespace.Clazz = Clazz.noConflict();
Development
Grunt is used to handle the build process for Clazz.js. To perform a full build, use the build
task:
grunt build
which is just an alias for the default
task:
grunt
To only check code quality and/or run unit tests use the test
task:
grunt test
License
Clazz.js is released under the MIT License