UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ WeakSet/ WeakSet.prototype.add()

The add() method of WeakSet instances appends a new object to the end of this WeakSet.

Syntax

add(value)

Parameters

Return value

The WeakSet object.

Exceptions

Examples

Using add

const ws = new WeakSet();

ws.add(window); // add the window object to the WeakSet

ws.has(window); // true

// WeakSet only takes objects as arguments
ws.add(1);
// results in "TypeError: Invalid value used in weak set" in Chrome
// and "TypeError: 1 is not a non-null object" in Firefox

Specifications

Browser compatibility

See also