A classical inheritence model with support for mixins
$ 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 = require'classes'Class;
// If using Node.js, the module needs to be required (unless global as shown above)var Class = require'classes'Class;// A general animal classClass'Animal'thisname = name;alertnoise;;// Create a class for making peopleClass'Person'Extends'Animal'thismakeNoisethisname + ' says: "' + sayWhat + '"';;// Create an instance of our person classvar james = 'James';// And make them talkjamesspeak'Hello, World';
There are multiple allowed syntaxes for extending classes, which you can choose at your preference.
// Create a base class firstClass'A';// Using the Extends() syntaxClass'B'ExtendsA;// Using the complex Class() syntaxClass'B' A;// Using the extend() syntaxAextend'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.
You can call the super of any method at any time. This is done using the
parent method on your class methods.
Class'A'ExtendsSomeOtherClass// Call the method's superthismethodparentthis;// Call this method's super, passing the argument alongthismethodWithArgsparentthis arg1;// Call the super, but pass in the arguments objectthisanotherMethodparentApplythis arguments;;
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.AnimalClass'Animal';// This class will not be declared globally, but instead just returnedvar Snake = ClassExtendsAnimal;// A slightly cleaner syntaxvar Snake = Animalextend// ...;
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 classClasssomeObject 'Animal';// And use the classvar animal = ;
This is equivilent to the following:
someObjectAnimal = Class;
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.
Class'Thing'Uses 'CanFoo'// ...;var thing = ;thingfoo;
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 = require'classes'Class;Classnamespaceexports;Class'Foo'// ...;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.