A collection of unique objects that may be of any type.
Syntax
setObj = new WeakSet()
Remarks
If you try to add a non-unique object to a WeakSet, the new object will not be added to the collection.
Unlike a Set, only objects may be added to the collection. Arbitrary values cannot be added to the collection.
In a WeakSet object, references to objects in the set are held 'weakly'. This means that WeakSet will not prevent a garbage collection from happening on the objects. When there are no references (other than WeakSet) to the objects, the garbage collector may collect the objects.
WeakSet (or WeakMap) may be helpful in some scenarios involving caching of objects or object metadata. For example, metadata for non-extensible objects may be stored in a WeakSet, or you may create a cache of user images using WeakSet.
Properties
The following table lists the properties of the WeakSet object.
| Property | Description |
|---|---|
| constructor | Specifies the function that creates a set. |
| prototype | Returns a reference to the prototype for a set. |
Methods
The following table lists the methods of the WeakSet object.
| Method | Description |
|---|---|
| add | Adds an element to a set. |
| delete | Removes a specified element from a set. |
| has | Returns true if the set contains a specified element. |
Example
The following example shows how to add members to a set and then verify that they have been added.
var ws = new WeakSet();
var str = new String("Thomas Jefferson");
var num = new Number(1776);
ws.add(str);
ws.add(num);
console.log(ws.has(str));
console.log(ws.has(num));
ws.delete(str);
console.log(ws.has(str));
// Output:
// true
// true
// false
Requirements
Supported in Microsoft Edge (Edge browser). Also supported in Store apps (Microsoft Edge on Windows 10). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.


