FWS. S2E2. Property order.
2 min readAug 10, 2020
Fun With Stamps. Season 2 — Best practices. Episode 2 — Property order.
Hello. I’m developer Vasyl Boroviak and welcome to the second season, second episode of Vasyl Boroviak presents Fun with Stamps.
In this season of FWS mini-articles we are going to learn stamp Best Practices using the stampit
npm module.
What’s the best ordering of the stamp parts?
TL;DR: order elements in the sequence of its execution.
Let’s start from an example. Please, read the comments.
The behaviouristic-utility stamp below counts the number of objects created from the stamp you compose it with.
// Firstly, other stamps/composables you want to mix with.
const HasInstanceCounter = stampit(SomeStamp, AnotherStamp, {
// Then give it a runtime name.
name: "HasInstanceCounter", // This line executed while this .js file is parsed and loaded.
conf: {
instanceCounter: 0
}, // This function is executed during composition, i.e.
// during HasInstanceCounter stamp creation.
composers({ stamp }) {
stamp.compose.configuration.instanceCounter = 0
},props: {
// This property will be assigned to object instance right
// before the initialiser below.
instanceIndex: -1
}, // This is called during object creation.
init(_, { stamp }) {
this.instanceIndex = stamp.compose.configuration.instanceCounter
stamp.compose.configuration.instanceCounter += 1
},methods: {
// Executed whenever you call this method.
getInstanceNumber() {
return this.instanceIndex
}
},statics: {
// Call it when you need to know a total number of instances,
// MyStamp.getNumberOfInstances()
getNumberOfInstances() {
return this.compose.configuration.instanceCounter
}
}
});
Let’s shorten this down to the ordered list:
- stamps you want to mix with (see above
SomeStamp, AnotherStamp
) - name
- configuration (rarely used feature)
- composers (rarely used feature)
- properties
- initializers
- methods
- statics (first — data properties, then — methods)
The rest of the episodes:
- Season one
- S2E1. Naming stamps
- S2E2. Property order (this article)