JavaScripture
Contribute via GitHub Feedback

JavaScript WeakSet : Object

WeakSets are a collection of Objects where each object can only appear once in the set similar to Set. Unlike Set, WeakSet does not allow iterating over its values. If WeakSet would be the only object holding on to the value, the value will be released from memory. The values stored in WeakSet cannot be primitive values (Boolean, Number, String, or undefined).

Constructors

new WeakSet() : WeakSet

Creates an empty WeakSet.

Example:

Run

Results:

 

new WeakSet(iterable : Object) : WeakSet

Creates a WeakSet by iterating over iterable and adding each value to the WeakSet.

Example:

Run

Results:

 

Instance Methods

add(value : Object) : Set

Stores value in this. If value is already stored, there is no change to the WeakSet. Returns this.

Example:

Run

Results:

 

clear() : undefined

Clears all values from this.

Example:

Run

Results:

 

delete(value : Object) : Boolean

Removes value* from this. Returns true if value was in this** before deleting it.

Example:

Run

Results:

 

has(value : Object) : Boolean

Returns true if the set contains value.

Example:

Run

Results: