stampit API
Search…
Initializers
You can add initializers (aka constructors) to your stamps.
1
const Logger = stampit({
2
init({level = 50}) {
3
this.level = level
4
}
5
})
6
7
const logger = Logger({ level: 42 })
8
9
logger.level === 42
Copied!
Each initializer will be executed on object creation. (But the list of initializers is always deduplicated.)
1
const Server = stampit(Logger, {
2
init({ port = process.env.PORT }) {
3
this.port = port
4
}
5
})
6
7
const server = Server({ port: 6666 })
8
9
server.level === 50
10
server.port === 6666
Copied!
If you return anything from an initializer then it becomes the object instance.
1
const NullServer = Server.init(function () {
2
return null
3
})
4
5
const server = NullServer()
6
7
server === null
Copied!

Descriptor merging algorithm

The initializers are concatenated into a deduplicated array. As the result, the order of composition becomes the order of initializer execution.
1
const {init} = stampit
2
3
const Log1 = init(() => console.log(1))
4
const Log2 = init(() => console.log(2))
5
const Log3 = init(() => console.log(3))
6
7
const MultiLog = stampit(Log1, Log2, Log3)
8
9
MultiLog() // Will print three times:
10
// 1
11
// 2
12
// 3
13
14
// because there are 3 initializers
15
MultiLog.compose.initializers.length === 3
Copied!
Stamps remove duplicate initializers.
1
const {init} = stampit
2
3
const func = () => console.log(1)
4
const Log1 = init(func)
5
const Log2 = init(func)
6
const Log3 = init(func)
7
8
const MultiLog = stampit(Log1, Log2, Log3)
9
10
MultiLog() // Will print only once:
11
// 1
12
13
// because there is only one initializer
14
MultiLog.compose.initializers.length === 1
Copied!

Initializer arguments

NOTE
Parameter - a variable being passed to a function.
Argument - a variable received by a function.
You can pass multiple parameters to your stamp while creating an object. First parameter passed as is to all initializers. But the rest of the parameters are available inside the {args} property of the second initializer argument.
1
const MultiArg = stampit({
2
init(arg1, { args }) {
3
arg1 === 'foo'
4
args[0] === arg1 // first argument of the initializer is passed from factory first parameter
5
args[0] === 'foo'
6
args[1] === 'BAR'
7
args[2] === 'thing'
8
}
9
})
10
11
MultiArg('foo', 'BAR', 'thing')
Copied!
If there is no first parameter then an empty object is passed as the first argument.
1
const NoException = stampit({
2
init({ iAmUndefined }) { // won't throw exception in this line
3
iAmUndefined === undefined
4
console.log(arguments[0]) // will print empty object: {}
5
}
6
})
7
8
NoException(/* nothing here! */)
Copied!
Every initializer second argument is always this object: { stamp, args, instance }. Where:
  • stamp is the stamp which was used to create this object. Useful to retrieve stamp's metadata (aka descriptor).
  • args the parameters passed to the stamp while creating the object.
  • instance the object instance itself. Always equals this context of the initializer.
1
const PrintMyArgs = stampit({
2
init(_, { stamp, args, instance }) {
3
console.log('Creating object from this stamp:', stamp)
4
console.log('List of arguments:', args)
5
console.log('Object instance:', instance)
6
7
this === instance
8
}
9
})
Copied!

Other ways to add initializers

Exactly the same stamp can be created in few ways. Here they all are.
1
function myInitializer ({ level = 50 }) {
2
this.level = level
3
}
4
5
const Logger = stampit({
6
init: myInitializer
7
})
8
9
const Logger = stampit({
10
init: [myInitializer]
11
})
12
13
const Logger = stampit({
14
initializers: myInitializer
15
})
16
17
const Logger = stampit({
18
initializers: [myInitializer]
19
})
20
21
const Logger = stampit.init(myInitializer)
22
const Logger = stampit.init([myInitializer])
23
const Logger = stampit.initializers(myInitializer)
24
const Logger = stampit.initializers([myInitializer])
25
26
const Logger = stampit().init(myInitializer)
27
const Logger = stampit().init([myInitializer])
28
const Logger = stampit().initializers(myInitializer)
29
const Logger = stampit().initializers([myInitializer])
Copied!
Last modified 3yr ago