WeakMap API Flashcards

1
Q

What is a WeakMap?

A

A WeakMap is a collection of key/value pairs whose keys must be objects or non-registered symbols, with values of any arbitrary JavaScript type, and which does not create strong references to its keys.

In a WeakMap, a key object refers strongly to its contents as long as the key is not garbage collected, but weakly from then on. As such, a WeakMap:

  • does not prevent garbage collection, which eventually removes references to the key object
  • allows garbage collection of any values if their key objects are not referenced from somewhere other than a WeakMap

A WeakMap can be a particularly useful construct when mapping keys to information about the key that is valuable only if the key has not been garbage collected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

WeakMap Constructor

A

The WeakMap() constructor creates WeakMap objects.

Syntax

new WeakMap()
new WeakMap(iterable)

Note: WeakMap() can only be constructed with new. Attempting to call it without new throws a TypeError.

Parameters

  • iterable - An Array or other iterable object that implements an @@iterator method that returns an iterator object that produces a two-element array-like object whose first element is a value that will be used as a WeakMap key and whose second element is the value to associate with that key. Each key-value pair will be added to the new WeakMap. null is treated as undefined.

Examples:

const wm1 = new WeakMap();
const wm2 = new WeakMap();
const wm3 = new WeakMap();
const o1 = {};
const o2 = function () {};
const o3 = window;

wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); // a value can be anything, including an object or a function
wm2.set(o3, undefined);
wm2.set(wm1, wm2); // keys and values can be any objects. Even WeakMaps!

wm1.get(o2); // "azerty"
wm2.get(o2); // undefined, because there is no key for o2 on wm2
wm2.get(o3); // undefined, because that is the set value

wm1.has(o2); // true
wm2.has(o2); // false
wm2.has(o3); // true (even if the value itself is 'undefined')

wm3.set(o1, 37);
wm3.get(o1); // 37

wm1.has(o1); // true
wm1.delete(o1);
wm1.has(o1); // false

WeakMap Constructor” Retrieved June 4, 2024.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

WeakMap.prototype.delete() method

A

The delete() method of WeakMap instances removes the specified element from this WeakMap.

Syntax

weakMapInstance.delete(key)

Parameters

  • key - The key of the element to remove from the WeakMap object.

Return value
true if an element in the WeakMap object has been removed successfully. false if the key is not found in the WeakMap. Always returns false if key is not an object or a non-registered symbol.

Examples:

const wm = new WeakMap();

wm.set(window, "foo");
wm.delete(window); // Returns true. Successfully removed.
wm.has(window); // Returns false. The window object is no longer in the WeakMap.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

WeakMap.prototype.get() method

A

The get() method of WeakMap instances returns a specified element from this WeakMap.

Syntax

get(key)

Parameters

  • key - The key of the element to return from the WeakMap object.

Return value
The element associated with the specified key in the WeakMap object. If the key can’t be found, undefined is returned. Always returns undefined if key is not an object or a non-registered symbol.

Examples:

const wm = new WeakMap();
wm.set(window, "foo");

wm.get(window); // Returns "foo".
wm.get("baz"); // Returns undefined.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

WeakMap.prototype.has() method

A

The has() method of WeakMap instances returns a boolean indicating whether an element with the specified key exists in this WeakMap or not.

Syntax

has(key)

Parameters

  • key - The key of the element to test for presence in the WeakMap object.

Return value
Returns true if an element with the specified key exists in the WeakMap object; otherwise false. Always returns false if key is not an object or a non-registered symbol.

Examples:

const wm = new WeakMap();
wm.set(window, "foo");

wm.has(window); // returns true
wm.has("baz"); // returns false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

WeakMap.prototype.set() method

A

The set() method of WeakMap instances adds a new element with a specified key and value to this WeakMap.

Syntax

set(key, value)

Parameters

  • key - Must be either an object or a non-registered symbol. The key of the entry to add to the WeakMap object.
  • value - Any value representing the value of the entry to add to the WeakMap object.

Return value

The WeakMap object.

Exceptions

Examples:

const wm = new WeakMap();
const obj = {};

// Add new elements to the WeakMap
wm.set(obj, "foo").set(window, "bar"); // chainable

// Update an element in the WeakMap
wm.set(obj, "baz");

// Using a non-registered symbol as key
const sym = Symbol("foo");
wm.set(sym, "baz");
wm.set(Symbol.iterator, "qux");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly