object-properties
    Preparing search index...

    Function defineLazyProperty

    • Defines a property that is initialized only when first accessed

      Type Parameters

      • T
      • P extends string
      • V

      Parameters

      • target: T
      • property: P

        name of the property

      • callback: LazyCallback<V>

        function called when the property is accessed the first time

      • Optionaloptions: DefinePropertyOptions
        • Optionalconfigurable?: boolean
        • Optionalenumerable?: boolean
        • Optionalwritable?: boolean

      Returns any

      target

      const obj = {};
      defineLazyProperty(obj, 'expensiveData', function() {
      return someExpensiveComputation();
      });
      // expensiveData is not computed until accessed
      console.log(obj.expensiveData); // Computes and returns the value