Logos DX
    Preparing search index...

    Function definePrivateProps

    • Defines hidden, non-configurable properties on an object.

      Creates properties that are not enumerable (hidden from for...in loops and Object.keys) and cannot be modified or deleted. Useful for internal state and private methods.

      Type Parameters

      • T
      • U extends Record<string, unknown>

      Parameters

      • target: T

        object to add properties to

      • props: U

        properties to define

      • configurable: boolean = false

        whether properties can be deleted or reconfigured

      Returns void

      const cache = new Map();
      const api = {};

      definePrivateProps(api, {
      _cache: cache,
      _getId: () => crypto.randomUUID()
      });

      console.log(Object.keys(api)); // [] (hidden properties)
      console.log(api._cache); // Map instance (accessible but hidden)
      class EventEmitter {
      constructor() {
      definePrivateProps(this, {
      _listeners: new Map(),
      _emit: this.emit.bind(this)
      });
      }
      }