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.
object to add properties to
properties to define
whether properties can be deleted or reconfigured
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) Copy
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) }); }} Copy
class EventEmitter { constructor() { definePrivateProps(this, { _listeners: new Map(), _emit: this.emit.bind(this) }); }}
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.