lang-def
Lightweight utility module for creating javascript classes.
copyright xkcd
Ok, Javascript already uses OOP, but based in prototypes. It is not bad, but occasionally the class system is the best approach. This library help you to create "raw vanilla" Javascript classes, for the best integrating with another codes and libraries with the best performance.
Features
- Great performance.
- Works in node.js and browsers
- In node.js use the native inherits.
- Use ES5 if is available, or the legacy way in legacy environment (cough cough IE<9).
- Support to classic inhiterance.
- Support to mixins.
- Support to CommonJs (like node.js), AMD (like requireJs) and as vanilla global module.
- Secure. If you forget to use the "new" operator to instantiate then TypeException is fired, so not fail silently.
- Elegant API & clean code base.
Quick Example
var def = var Point = var a = 2 4var b = 5 -6 a;// -> 10.44030650891055
Installation
npm:
$ npm install lang-def
$ bower install lang-def
Or simply download.
Import
In CommonJS (like node.js)
var def = require('lang-def')
In AMD (like requireJS)
require;
Then simply use
;
In vanilla (like simple web page)
API
def
Creates a constructor function (Class).
Signature:
def([ string name ], [ function BaseClass ], [ array mixins ], [ object props ]) -> Function
- optional string name:
- The name of the class. Useful for debugging.
- default:
undefined
, but resolved to'AnonymousConstructor'
. - limitations:
- Must be compilant with the general rules for Javascript variables,
i.e.
'Person'
,'SuperPerson'
. Not'persons.Person'
or2Person
. - Is compatible with Chrome Extensions,
but not with Chrome Apps, so for developing Chome Apps omit the name. This restriction is due to the use of
new Function(...)
to generate the name in the runtime.
- Must be compilant with the general rules for Javascript variables,
i.e.
- optional function BaseClass:
- The parent constructor in the classic inhiterance.
- default:
undefined
, by definition inherits fromObject
. - limitations:
- Can be a constructor function (Class) created by
def
or by vanilla javascript. - Currently no work well for DOM Interfaces, like HTMLDivElement.
- Can be a constructor function (Class) created by
- optional array mixins:
- An array of constructor functions that is mixed from left to right (the right overrides the left).
- default:
[ ]
- limitations:
- The same of BaseClass.
- optional object props:
- The properties or members of the Class to create.
- default:
{ }
- especial members:
- optional function new: is the
constructor
.- default:
function () { def.mixin(this, arguments); }
.
- default:
- (in each method) this.fn(function Class [, string methodName]):
- return the function
methodName
ofClass
in thethis
clausure.
- return the function
- optional function new: is the
- limitations:
- Must not contain one of the follows special members: mixins_, super_ and fn.
Examples
A little Game Egine
See in jsfiddle.net/mnrtag0v/8
.
Classic examples
Inheritance
var Animal = ; var Cat = ; var Dog = ; var cat = name: 'Canela' ;var dog = name: 'Doky' ; cat instanceof Cat;// -> truecat instanceof Animal;// -> true dog instanceof Dog;// -> truedog instanceof Animal;// -> true cat;// -> 'I am Canela, the Cat'
Mixins
Example inspired from dgrid
var Widget = ;var Grid = ; var Sortable = ;var Selectable = ;var Resizable = ; // Then we need a sortable gridvar MySortableGrid = ; // Then we need a sortable and selectable gridvar MySortableAndResizable = ; // one gridvar grid = ... ; grid instanceof Grid; // -> truegrid instanceof Widget; // -> truegrid instanceof Sortable; // -> falsegrid instanceof Selectable; // -> false def; // -> truedef; // -> truedef; // -> true, because is Mixindef; // -> true, because is Mixin def; // -> falsedef; // -> falsedef; // -> truedef; // -> true
def.mixin
Mixin arg0 with arg1, then with arg2 (if exits), etc.
Signature:
def -> object
- object arg0
- ...
The arg0 object is mutated and returned.
def.mixinOf
Check if instance is mixin of Mixin.
Signature:
def
- object instance
- function Mixin
def.instanceOf
Check if instance is instance or mixin of MixinOrClass.
Signature:
def
- object instance
- the object to check
- limitations: none
- function MixinOrClass
- the constructor function
- limitations: none, can be a vanilla function.
Best Practies & Tips
- Feel free to use
def
with vanilla constructor functions and in library proyects.def
just construct a vanilla constructor, so is not intrusive. - In CommonJS or AMD environment use one file per Class.
- Use the conventional prefix
_
for private members. Example_lastPosition: 5
. We can over engineering this lib for have a real private concept, but then we like some way for made reflection... so, KISS is the better. - To define static properties add them to the constructor. i.e.
XHR = def(...); XHR.TYPE_OK = 1; XHR.TYPE_ERR = 0
.
TODO
- Experimenting................100%
- Writing the code.............100%
- Anecdotally Testing..........100%
- Automated Testing on Travis..75%
- Documenting the API..........90%
LICENSE
MIT LICENSE