Explicit define a lazy initializer property for an object or class prototype.
The prototype or object contains the property.
The key of the property.
The init function, which will returns the value once initialized.
Writable flag for the property.
Configurable flag for the property after initialized.
Enumerable flag for the property.
Enforce to seal (set non-configurable) the lazy initializer.
If this is set to true
, the initializer will not reconfigure itself
and the initialized value will be store to a hidden heap when you get/set it directly
(not via an inherited instance with the target
as prototype).
This is safer but slower therefore it is not recommend to set true
if you want to define
lazy property on an object but not as an prototype for inheritance.
Explicit define lazy initializer properties for an object or class prototype.
The prototype or object contains the property.
Key hash for all descriptors would like to define.
Transform a dynamic property to lazy initializer. Alternative method for those environment which does not support decorators.
The target class to work with.
The key of the properties would like to transform.
Generated using TypeDoc
Decorator to transform applied property getter to lazy initializer. Lazy properties are undefined until the first interaction, then it will become static.
class Schrodinger { @LazyProperty get cat() { return Math.random() > 0.5; } // Setter will be called when the value has been assigned first time. // Setters can be not defined, but then the property will be read-only. set cat(value) { console.log(`It is ${value ? 'alive' : 'dead'} now!`); assert.strictEqual(value, this.cat); } }