Introduction
Last updated
Last updated
stampit
is a JavaScript module which implements the stamp specification. Stamps are composable factory functions.
Think of stamps as classic classes but without any limits, boundaries, or rules.
The stamps brag large amount of features and things you can do with your objects and/or stamps.
Head straight to the Quick Start page for code examples.
Inheritance
Classes
Classes do child+parent+parent+parent... inheritance chains. See picture above.
When you extend a class you link class prototypes.
Stamps
Stamp are single object + single prototype. See picture above.
When you "extend" you separately merge methods, separately merge properties, separately merge static properties, separately merge initializers (aka constructors), etc etc etc.
That's why it is not just inheritance, but special type of object composition. The stamp composition algorithm is standardized.
You can influence composition result with your code at runtime using the composers feature.
Object creation
Classes
In most programming languages you execute only one constructor per class.
To pass data to the parent constructor you have to manually call parent constructor this.super(data)
.
To create an object you need to use the new
keyword: const object = new MyClass()
.
Stamps
Stamp executes every initializer (aka constructor) it has.
All initializers receive exactly the same set of arguments, no need to manually pass data.
The initializer execution sequence is the same as the stamp composition sequence.
To create an object you call stamp as a function: const object = MyStamp()
.
The original idea of Stamps belongs to Eric Elliott. See his first commit from 11 Feb 2013 in the stampit repository. Since then the idea evolved to the specification and a whole ecosystem of mini modules.
Head straight to the Quick start for code examples.
Stampit have a lot of helper modules. You can always find the list of official NPM packages here: https://www.npmjs.com/~stamp
See more information on the Ecosystem page.
The example below introduces GraphPoint
and GraphLine
stamps (aka blueprints, aka factory functions, aka behaviours).
Let's start by declaring the Point
stamp.
That's how you create instance of that stamp.
Now let's "inherit" the Point
to create a Circle
. We will add radius
property to the mix.
When creating instance of the Circle
you will actually call TWO different initialisers (aka constructors). Stamps have multiple initialisers.
Now, declaring couple of additional stamps. We'll use them to enrich JavaScript drawable objects.
The Drawable
behaviour. We have to make sure that developers implement the draw()
method when using this stamp.
Now let's implement the GraphPoint
stamp.
Please, note we are executing FOUR intializers here.
Line
primitive.
Our final destination - a drawable graph line.
And the usage.
The idea is - separate concerns. We split our logic into several reusable highly decoupled primitive behaviours (Point, Colored, Tagged, Circle, Line).
The above has:
Point
concern and all related functionality in a separate stamp.
Circle
concern and the related functionality separated. Circle
is an extension of the Point
.
Tagged
concern in a separate stamp.
Colored
concern in a separate stamp.
GraphPoint
concern is Circle
, Tagged
and Colored
concerns combined.
Line
concern - it has two Points
.
GraphLine
- is the Line
but with additional Tagged
and Colored
functionalities.
You can't do anything like that with classes. And the resulting code is more readable than classes.