Initializers
You can add initializers (aka constructors) to your stamps.
const Logger = stampit({
init({level = 50}) {
this.level = level
}
})
const logger = Logger({ level: 42 })
logger.level === 42Each initializer will be executed on object creation. (But the list of initializers is always deduplicated.)
const Server = stampit(Logger, {
init({ port = process.env.PORT }) {
this.port = port
}
})
const server = Server({ port: 6666 })
server.level === 50
server.port === 6666If you return anything from an initializer then it becomes the object instance.
Descriptor merging algorithm
The initializers are concatenated into a deduplicated array. As the result, the order of composition becomes the order of initializer execution.
Stamps remove duplicate initializers.
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.
If there is no first parameter then an empty object is passed as the first argument.
Every initializer second argument is always this object: { stamp, args, instance }. Where:
stampis the stamp which was used to create this object. Useful to retrieve stamp's metadata (aka descriptor).argsthe parameters passed to the stamp while creating the object.instancethe object instance itself. Always equalsthiscontext of the initializer.
Other ways to add initializers
Exactly the same stamp can be created in few ways. Here they all are.
Last updated