1.1.4 • Published 3 months ago
@squeep/lazy-property v1.1.4
@squeep/lazy-property
Defer property initialization until first access.
API
lazy(obj, name, initializer, descriptor, objectBound = true)
Add name
property to obj
, which will invoke initializer
on first access
and update name
with the resulting value.
Property settings can be specified with descriptor
, as in Object.defineProperty()
.
If objectBound
is true and the lazy property is set on a prototype object,
the property will be updated on the prototype object. If objectBound
is false
and on a prototype object, the property will be updated on the inherited object
and the initializer
will be invoked once for each inherited object accessed.
The this
object in initializer
will be explicitly obj
when objectBound
is set,
otherwise it will be this
.
Returns obj
.
Example
const { lazy } = require('@squeep/lazy-property');
const obj = {};
lazy(obj, 'prop', () => doSomethingExpensive());
const getItNow = obj.prop;