stampit API
Search…
Static properties
Static properties are properties attached to the stamp itself.
1
const PrintMyself = stampit({
2
statics: {
3
myProperty: 'foo',
4
5
printMyself() {
6
console.log(this)
7
}
8
}
9
})
10
11
PrintMyself.myProperty === 'foo'
12
13
PrintMyself.printMyself() // call the function without creating an instance of the stamp
Copied!
If you compose the stamp above with any other stamp, then it will have the printMyself method too.
1
const Server = stampit(PrintMyself, {
2
...
3
})
4
5
Server.printMyself()
Copied!
Fun fact
Stampit's chain methods are all regular static properties: Stamp.props().methods().init().statics().conf()... etc

Using statics to compose in your own way

Often static methods are used to tweak a stamp, to compose more metadata in a domain specific way.
1
const HasFactor = stampit()
2
.statics({
3
allowFactorSetter(allow) {
4
return this.conf({ HasFactor: { allowSetter: allow } }) // tweaking the stamp. Using conf() static method
5
}
6
})
7
.init(({ factor }, { instance, stamp }) => {
8
let factor = factor || 1 // private (scoped) variable
9
instance.getFactor = () => factor
10
11
const { configuration } = stamp.compose
12
if (configuration && configuration.HasFactor && configuration.HasFactor.addFactorSetter) {
13
instance.setFactor = f => { factor = f }
14
}
15
})
Copied!
The example above exposes a static function which controls access to the setFactor method of your object instances.
1
HasFactor().setFactor === undefined // there is no .setFactor() method
2
3
const HasFactorAllowed = HasFactor.allowFactorSetter(true)
4
const instance = HasFactor2()
5
instance.setFactor(5); // we have access to the setter!
6
instance.getFactor() === 5 // the factor was set
Copied!

Descriptor merging algorithm

The static properties are copied by assignment. In other words - by reference using Object.assign.
1
HasFactor.allowFactorSetter === HasFactorAllowed.allowFactorSetter
Copied!
In case of conflicts the last composed property wins.

Other ways to add static properties

Exactly the same stamp can be created in few ways. Here they all are.
1
function func(allow) {
2
return this.conf({ HasFactor: { allowSetter: allow } })
3
}
4
5
const HasFactor = stampit({
6
statics: {
7
allowFactorSetter: func
8
}
9
})
10
11
const HasFactor = stampit({
12
staticProperties: {
13
allowFactorSetter: func
14
}
15
})
16
17
const HasFactor = stampit.statics({
18
allowFactorSetter: func
19
})
20
21
const HasFactor = stampit.staticProperties({
22
allowFactorSetter: func
23
})
24
25
const HasFactor = stampit().statics({
26
allowFactorSetter: func
27
})
28
29
const HasFactor = stampit().staticProperties({
30
allowFactorSetter: func
31
})
Copied!