The Set
object allows for storing unique values of any type, whether they are primitive values or object references. The values in a Set
object are unique and do not repeat.
The WeakSet
object allows for storing unique values of object weak references. The values in a WeakSet
object are also unique and can only store weak references of objects.
The Set
object is a collection of values that allows iterating over its elements in the order of insertion. The elements in a Set
only appear once, meaning the elements are unique and it is commonly used to remove duplicates from an array.
Set.prototype.constructor
: Returns the constructor function.Set.prototype.size
: Returns the number of values in the Set
object.Set.prototype.add(value)
: Adds an element to the end of the Set
object and returns the modified Set
object.Set.prototype.clear()
: Removes all elements from the Set
object.Set.prototype.delete(value)
: Removes the element from the Set
that is equal to the specified value.Set.prototype.entries()
: Returns a new iterator object containing the [value, value]
arrays of all elements in the Set
object, ordered by insertion, to keep the method similar to Map
objects where each value's key and value are equal.Set.prototype.forEach(callbackFn[, thisArg])
: Invokes the callback
for each value in the Set
object in the order of insertion. If the thisArg
parameter is provided, this
in the callback will be that parameter.Set.prototype.has(value)
: Returns a boolean value indicating whether the value exists in the Set
or not.Set.prototype.keys()
: Returns a new iterator object containing all the values of the Set
object, ordered by insertion.Set.prototype.values()
: Returns a new iterator object containing all the values of the Set
object, ordered by insertion.Set.prototype[@@iterator]()
: Returns a new iterator object containing all the values of the Set
object, ordered by insertion.The values in a WeakSet
can only be of type Object
, holding weak references to objects. Primitive data types cannot be used as values. WeakSet
holds weak references to objects, meaning that garbage collection can be performed correctly when there are no other references. The references stored in a WeakSet
are only valid as long as the referenced objects have not been collected, and due to weak references, WeakSet
is not enumerable.
In simple terms, there are times when we need to store some objects on an array, but this would create a reference to the object. Once the object is no longer needed, we would have to manually remove the reference, otherwise the garbage collection mechanism would not release the memory occupied by the object. The design of WeakSet
resolves this issue as the referenced objects are weakly referenced, meaning the garbage collection mechanism does not consider these references, so as long as all the other references to the referenced objects are cleared, the garbage collection mechanism will release the memory occupied by the object and the corresponding object reference in the WeakSet
will disappear without the need for manual reference deletion. If there is a need to add objects to an array without interfering with the garbage collection mechanism, WeakSet
can be used. Additionally, WeakSet
is particularly suitable for tracking object references, especially when dealing with a large number of objects.
WeakSet.prototype.constructor
: Returns the constructor function.WeakSet.prototype.add(value)
: Adds a new element value
to this WeakSet
object.WeakSet.prototype.delete(value)
: Deletes the element value
from this WeakSet
object.WeakSet.prototype.has(value)
: Returns a boolean value indicating whether the given value value
exists in this WeakSet
.