stampit API
Search…
Properties
You can add properties to the objects created from your stamps. These are default properties of your objects.
1
const HasLog = stampit({
2
props: {
3
log: require('bunyan').createLogger({ name: 'my logger' })
4
}
5
})
6
7
const loggerObject = HasLog()
8
loggerObject.log.debug('I can log')
Copied!
If you compose the stamp above with any other stamp, then object instances created from it will also have the .log property.
1
const RequestHandler = stampit(HasLog) // composing with HasLog
2
.methods({
3
handle(req, res, next) {
4
this.log.info({ originalUrl: req.originalUrl }, 'handling request') // using the .log
5
res.sendStatus(200)
6
}
7
})
8
9
const handler = RequestHandler()
10
handler.log.debug('Created a handler')
Copied!

Descriptor merging algorithm

The properties are copied by assignment. In other words - by reference using Object.assign.
1
HasLog().log === RequestHandler().log
Copied!
In case of conflicts the last composed property wins.

Other ways to add properties

Exactly the same stamp can be created in few ways. Here they all are.
1
const myLogger = require('bunyan').createLogger({ name: 'my logger' })
2
3
const HasLog = stampit({
4
props: {
5
log: myLogger
6
}
7
})
8
9
const HasLog = stampit({
10
properties: {
11
log: myLogger
12
}
13
})
14
15
const HasLog = stampit.props({
16
log: myLogger
17
})
18
19
const HasLog = stampit.properties({
20
log: myLogger
21
})
22
23
const HasLog = stampit().props({
24
log: myLogger
25
})
26
27
const HasLog = stampit().properties({
28
log: myLogger
29
})
Copied!
Last modified 3yr ago