alter-ego
This library provides simple but very extensible way to define types / classes / constructors in JS. It comes a very small set of functions to do that, but any function can be incorporated to allow more complex definitions.
Usage
It's quite common to have constructors that just set given arguments as properties of the instance, providing rest of the API through prototype methods. This library makes this task simple by reducing all the machinery code:
define record
var Point =
Code above will produce constructor which is equivalent of:
var { thisx = x thsy = y}
define prototype
Commonly you will want to define some methods for the Point
type, that
can be done as follows:
var Point =
extend type
You could also extend existing types in similar way:
var Pixel =
custom constructor
Now record
is limited to a very specific scenarios, but more generic use
cases are also covered:
var Dog =
refine existing types
You could also refine existing types in a same way:
Extension
While this tiny library provides some core functionailty for building and
extending types / constructors / classes, there is no way it could cover all
use cases. That's not an issues since all the functionality is defined in
via simple functions like extend
, implement
, record
... You could
incorporate your own functions to do more sophisticated things. For example
adding aspect oriented programming
concepts is very easy:
{ var prototype = typeprototype Object return type} var Point3D = // EventEmitter2
You could in fact do so much more! Enforce API contracts using guards, define extensions on built-ins types without changing their prototypes via type based dispatch or do a pattern based dispatch or maybe something entirely new!
Install
npm install alter-ego