class.js
Simple but powerful classical inheritence for JavaScript.
Node.js install
$ npm install classes
If using Node.js, you may want to make classes global using code such as this near the beginning of your application.
global.Class = require('classes').Class;
Basic Example
var Class = require('classes').Class;
Class('Animal', {
construct: function(name) {
this.name = name;
},
makeNoise: function(noise) {
alert(noise);
}
});
Class('Person').Extends('Animal', {
speak: function(sayWhat) {
this.makeNoise(this.name + ' says: "' + sayWhat + '"');
}
});
var james = new Person('James');
james.speak('Hello, World');
Extending Classes
There are multiple allowed syntaxes for extending classes, which you can choose at your preference.
Class('A', {
...
});
Class('B').Extends(A, {
...
});
Class('B', A, {
...
});
A.extend('B', {
...
});
Also note, that when using the Extends()
syntax or the complex Class()
syntax, both a parent class variable (A
) or string ('A'
) is allowed, but if you use the string syntax, the parent class must exist on the namespace object.
Using Super
You can call the super of any method at any time. This is done using the parent
method on your class methods.
Class('A').Extends(SomeOtherClass, {
method: function() {
this.method.parent(this);
},
methodWithArgs: function(arg1) {
this.methodWithArgs.parent(this, arg1);
},
anotherMethod: function() {
this.anotherMethod.parentApply(this, arguments);
},
});
Creating Anonymous Classes
Using class.js, classes don't have to be assigned a name. You can also tell the Class()
function to simply return the constructed class function by passing a falsey first param (like Class(null)
) or by simply not giving one as seen below.
Class('Animal', {
...
});
var Snake = Class().Extends(Animal, {
...
});
var Snake = Animal.extend({
});
Defining Classes in Non-Global Scope
To define a class, but assign it somewhere other that the global object, you pass in a two key array as the class name. The first value is the object to define the class on, and the second is the class name.
var someObject = { };
Class([someObject, 'Animal'], {
...
});
var animal = new someObject.Animal();
This is equivilent to the following:
someObject.Animal = Class({
...
});
Using Mixins
As of version 0.2.0, mixins are supported. It should be noted that mixins are not the same as sub-class inheritence. A single class can implement both a parent class as well as mixins. Mixins are defined using the Class.mixin
method.
Class.mixin('CanFoo', {
foo: function() {
alert('Foo!');
}
});
Once created, a mixin is used with the uses
method when defining a class.
Class('Thing').Uses([ 'CanFoo' ], {
});
var thing = new Thing();
thing.foo();
Mixins are different from inheritence in the sense that they do no add to the inheritence chain, they simply extend the current class with certain functionality. You cannot use instanceof
to determine mixin inheritence because classes are not instances of mixins; In fact, there is no such thing as an instance of a mixin, they are just objects.
Using Namespaces
Before version 0.2.0, all new classes and mixins were defined, by default, on the global object, and if you wanted to define one elsewhere, you would have to use either anonymous classes or the array syntax (eg. Class([exports, 'Foo'], ...)
). There is now a new way of defining namespaced classes that should prove useful, especially in the case of Node.js.
var Class = require('classes').Class;
Class.namespace(exports);
Class('Foo', {
});
var foo = new exports.Foo();
The Class.namespace()
function sets the default namespace, allowing shorter, more readable class declarations. Simply call Class.namespace(exports)
at the top of your modules and your classes will automatically be defined in the correct space.