Sometimes when programming in JavaScript you'll want to use Classes. Because I was not happy with all the class systems available for JavaScript I created this simple class system for JavaScript.
Use, distribute and modify the code however you want. A simple acknowledgement would be great, but not required.
- Namespaces
- Basic Inheritance
- Mixins
- Static Properties
- Class manager
Class(ClassName, ClassDefinition);
Where ClassName is a string and defines the name of the class, and ClassDefinition is an object the contains the class definition. Both of these arguments are required to create an class.
There are some keywords that are used internally, which should not be used in the class definition, because they will be overriden on class creation.
- constructor
- extends
- static
- mixins
- super
- className
- instanceNumber
- initMixins
- initConfig
- isInstanceOf
- getInheritance
Class("Test", {
static: {
STATIC_VAR: 'VALUE'
},
extends: OtherClass,
mixins: [MixinClass],
constructor: function(){
// call super constructor
this.super.constructor.call(this);
console.log("constructor called");
}
});
var testInstance = new Test();
A Mixin is a class which can do everything a normal class can do, but has to implement a initMixin function, which will be called automatically on construction of an instance.
Class("MixinClass", {
initMixin: function(){
console.log("init mixin");
}
});
Static properties have to be defined as a object as a static property in the class definition.
Class("StaticTest", {
static: {
STATICVAR: 'value'
}
});
var staticvalue = StaticTest.STATICVAR;
You can also inherit from other classes by using the extends keyword in the class definition. If you are overriding the constructor, you will probably want to call the constructor of the parent class, which you can do like this:
this.super.constructor.call(this);
or like this (where ClassName is the name of the class):
ClassName.super.constructor.call(this);
You can also always get the parent classes of an class, by calling getInheritance of that class.
- getInheritance will return the names of the parent classes as an array of string.
- getInheritance(true) will return the parent class as an array of classes
You can check if an instance is an instance of a specific class by using the native javascript instanceof method, or by calling isInstanceOf(Class) of an class. When using isInstanceOf you can also pass the class name as an string.
Every class that is defined will be registered in the class manager, you can check if a class is defined, and even get a class by it's name.
- addClass(className, class): adds a class to the class manager
- removeClass(className): removes a given class from the class manager
- getClass(className): retrieves a class by it's name
- hasClass(classname): checks if a class by the given name exists