$ 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.
globalClass = Class;
// If using Node.js, the module needs to be required (unless global as shown above)var Class = Class;// A general animal class;// Create a class for making people;// Create an instance of our person classvar james = 'James';// And make them talkjames;
There are multiple allowed syntaxes for extending classes, which you can choose at your preference.
// Create a base class first;// Using the Extends() syntax;// Using the complex Class() syntax;// Using the extend() syntaxA;
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.
You can call the super of any method at any time. This is done using the
parent method on your class methods.
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.
// This class will automatically be declared at global.Animal;// This class will not be declared globally, but instead just returnedvar Snake =;// A slightly cleaner syntaxvar Snake = Animal;
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.
// This is where we will put the classvar someObject = ;// Now define the class;// And use the classvar animal = ;
This is equivilent to the following:
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
Once created, a mixin is used with the
uses method when defining a class.
;var thing = ;thing;
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.
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 = Class;Class;;var foo = ;
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.