Options
All
  • Public
  • Public/Protected
  • All
Menu

Module LazyProperty

Callable

  • LazyProperty<T, K>(target: T, key: K, attr: TypedPropertyDescriptor<T[K]>): TypedPropertyDescriptor<T[K]>
  • LazyProperty<T>(target: object, key: PropertyKey, attr: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>
  • Decorator to transform applied property getter to lazy initializer. Lazy properties are undefined until the first interaction, then it will become static.

    example
    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);
      }
    }

    Type parameters

    • T: object

    • K: keyof T

    Parameters

    • target: T
    • key: K
    • attr: TypedPropertyDescriptor<T[K]>

    Returns TypedPropertyDescriptor<T[K]>

  • Type parameters

    • T

    Parameters

    • target: object
    • key: PropertyKey
    • attr: TypedPropertyDescriptor<T>

    Returns TypedPropertyDescriptor<T>

Index

Functions

Functions

define

  • define<T, K>(target: T, key: K, init: LazyInit<T, K>, writable?: undefined | false | true, configurable?: undefined | false | true, enumerable?: undefined | false | true, sealed?: undefined | false | true): Defined<T, K>
  • define<T, K>(target: T, defines: DefineDescriptors<T, K>): Defined<T, K>
  • Explicit define a lazy initializer property for an object or class prototype.

    example
    const someObject = {};
    LazyProperty.define(someObject, 'somelazyField', () => 'boo!');

    Type parameters

    • T: object

    • K: keyof T

    Parameters

    • target: T

      The prototype or object contains the property.

    • key: K

      The key of the property.

    • init: LazyInit<T, K>

      The init function, which will returns the value once initialized.

    • Optional writable: undefined | false | true

      Writable flag for the property.

    • Optional configurable: undefined | false | true

      Configurable flag for the property after initialized.

    • Optional enumerable: undefined | false | true

      Enumerable flag for the property.

    • Optional sealed: undefined | false | true

      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.

    Returns Defined<T, K>

  • Explicit define lazy initializer properties for an object or class prototype.

    example
    const someObject = {};
    LazyProperty.define(someObject, {
      someOtherLazyField: () => 'another one!',
      someMoreComplicatedLazyField: {
        init: () => 'More controllable behaviour!',
        enumerable: false,
        configurable: false,
        writable: true,
      },
    });

    Type parameters

    • T: object

    • K: keyof T

    Parameters

    • target: T

      The prototype or object contains the property.

    • defines: DefineDescriptors<T, K>

      Key hash for all descriptors would like to define.

    Returns Defined<T, K>

transform

  • transform<T, C>(target: C, ...keys: Array<keyof T>): C
  • Transform a dynamic property to lazy initializer. Alternative method for those environment which does not support decorators.

    example
    class Schrodinger {
      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);
      }
    }
    LazyProperty.transform(Schrodinger, 'cat');

    Type parameters

    Parameters

    • target: C

      The target class to work with.

    • Rest ...keys: Array<keyof T>

      The key of the properties would like to transform.

    Returns C

Generated using TypeDoc