compose-class
Composition over inheritance
Utility function that allows you to compose a class using mixins and decorators
npm install --save compose-class
Motivation
The idea behind this package is to use mixins and decorators as buildings blocks and use them across an aplication code base.
Usage
Quick start
; const Class = ; const instance = 'Tom'; console; // 'Tom'
Using mixins
This is the main motivaton of writing this package.
; const WalkMixin = { console; } ; const TalkMixin = { console; } ; const Class = ; const tom = 'Tom'; tom; // 'Tom is walking' tom; // 'Tom is talking'
With mixin initialization
If mixin has defined constructor
it will be invoked before main class constructor.
// entity-mixin.js ; const ID = Symbol'id'; let counter = 0; { counter += 1; thisID = counter; } { return thisID; } ;
// user.js ; ; ; const FIELDS = name: Symbol'name' ; const User = ; const u1 = 'Tom'; console; // '1'; console; // 'Tom' const u2 = 'Jerry'; console; // '2'; console; // 'Jerry'
Decorators
Sometimes using mixins is not enough to build a complex type wtih many rules. In order to execute pre/post conditions against type methods we need to wrap them. Decorators is the best tool for it.
In order to apply decorator to an instance, it needs to pass decorator factory to decorators
array which accepts 2 arguments: name and function.
// assert-decorator.js { if name < 0 return method; return { if !value throw 'Value is missed'; return method; }; }
// user.js ; ; ; ; const FIELDS = name: Symbol'name' ; const User = ; const u1 = 'Tom'; console; // '1'; console; // 'Tom' u1; // exception
Statics
Using statics
object it's possible to define static methods of a type.
; ; const User = ; User;