dos -- DELIGHT object system
An advanced OO-Framework for JavaScript. This framework was used in production in the DELIGHT engine. Supports both node/io environments as well as all major browsers using browserify.
Install
To install just use npm.
npm install dos
Example
A small example of defining and using a class in dos.
var dos = ;var namespace = {};dos;// use it right awayvar myObject = ;// or export for later usemoduleexports = namespace;
Functionality
Just a quick overview of what you can do with dos. This section is very rough and incomplete as of now. Feel free to ask and/or browse the code.
Defining
The main functionality of the dos is to define classes. There are different kinds of classes that you can define:
Normal Classes
Your standard classes that everybody should know.
dos;
Abstract Classes
Classes that cannot be instantiated using new -- they can contain interface methods.
dos;
Static Classes
Classes that only have a static part, most commonly used as util holders.
dos;
Interfaces
Interfaces are orthogonal to the class hierarchy. They cannot contain data. You can implement multiple interfaces in one class.
dos;
Exceptions
Wrapping Error objects in dos. This functionality comes in handy when serializing exceptions (not covered in this guide),
dos;
Allocators
In dos you can control how new behaves on a class. The default is a PoolAllocator, but you can write custom ones using this notation. See below for a small example on how to subclass PoolAllocator.
dos;
Class Descriptor
In dos the actual class description is encoded in a class descriptor. Here are some of the most important options you can use. There are more advanced ones that this small guide does not cover. Please ask and/or look at the code for more information.
Extends
Simple single inheritance using the extends option.
var ns = {};dos;
Implements
Simple implementation of multiple interfaces using implements.
var ns = {};dos;
Non-Static
The $ sign holds a class definition function for all non-static members. There are many things that you can define inside of the function, most of which are outlined below. The parameters to the function are:
This A reference to the object.
Super A reference to the super class members
Public The public class definition functions of the class.
Protected The private class definition functions of the class.
Private The private class definition functions of the class.
var ns = {};dos;
Static
Most notably you define nested classes in the static part of your class definition. You can also define constants and even create a constant of the same type as the class that you are currently defining.
var ns = {};dos;
Interface
In interfaces you just write $method,$getter and so on but not supply an implementation. You will have to implement those in classes that use the interfaces.
var ns = {};dos;
Allocators
Custom allocators allow you to control how objects are managed between init and dispose. The default should work well for most cases, but if you for example want to get some allocation pattern info for debugging purposes, you can subclass an Allocator.
var ns = {}; dos dos; ; // uses CustomAllocator
RTTI
The dos has a rich set of run-time type information features. Please note that they work on instances of as well as classes.
var ns = {};dos; nssubnsClass;nssubnsClass; var foo = ; foo; // truefoo; // true SomeBaseClass; // outputs array: [ ns.subns.Class ]SomeInterface; // outputs array: [ ns.subns.Class ] nssubnsClassclass; // this is the class ctor nssubnsClassclassName; // "subns.Class" nssubnsClasssimpleClassName; // "Class" nssubnsClassclassHierarchy; // outputs array: [ ns.subns.Class.class, SomeBaseClass.class ];
Params
The dos works with named parameters. The init method (or constructor) of dos classes has a magic Params object which helps you validate the parameters given to the class at construction time.
Here are some examples, but there are more things you can do with it like extend on the validator pattern to write your own parameter validation.
var ns = {};dos;
Tests
To run unit and coverage tests do the following on your favorite shell:
# install dev dependencies npm install -d# test in a browser npm run test# test in node npm run testNode# show test coverage npm run coverage
Credits
(c) 2012-2015 xymatic GmbH. MIT License. See LICENSE file for more information.