Skip to content

WeakMap

A WeakMap object is a collection of key/value pairs in which keys must be objects. References to key objects in a WeakMap are held weakly, meaning they do not prevent garbage collection if there are no other references to the object. WeakMap keys are not iterable.

set(key, value) function

Set the value for the specified key in the WeakMap. The key must be an object.

key: The object key.

value: The value associated with the key.

Returns: The WeakMap object (for chaining).

get(key) function

Return the value associated with 'key' in the WeakMap, or undefined if no such key exists. The key must be an object.

key: The object key.

Returns: The value associated with the key, or undefined if not present.

has(key) function

Return a boolean indicating whether an element with the specified key exists in the WeakMap. The key must be an object.

key: The object key to check.

Returns: True if the key is found, otherwise false.

delete(key) function

Remove the specified key and its associated value from the WeakMap, if it exists.

key: The object key to remove.

Returns: True if an element was removed, otherwise false.