WeakSet() constructor
En esta página
The WeakSet() constructor creates {{jsxref("WeakSet")}} objects.
Syntax#
new WeakSet()
new WeakSet(iterable)
[!NOTE]
WeakSet()can only be constructed withnew. Attempting to call it withoutnewthrows a {{jsxref("TypeError")}}.
Parameters#
iterable{{optional_inline}}- : If an iterable object is passed, all of its elements will be added to the new
WeakSet.nullis treated asundefined.
Examples#
Using the WeakSet object#
const ws = new WeakSet();
const foo = {};
const bar = {};
ws.add(foo);
ws.add(bar);
ws.has(foo); // true
ws.has(bar); // true
ws.delete(foo); // removes foo from the set
ws.has(foo); // false, foo has been removed
ws.has(bar); // true, bar is retained
Note that foo !== bar. While they are similar objects, they are not
the same object. And so they are both added to the set.
Specifications#
{{Specifications}}
Browser compatibility#
{{Compat}}
See also#
- Polyfill of
WeakSetincore-js - {{jsxref("WeakSet")}}