24.4 WeakSet Objects
WeakSets are collections of objects and/or symbols. A distinct object or symbol may only occur once as an element of a WeakSet's collection. A WeakSet may be queried to see if it contains a specific value, but no mechanism is provided for enumerating the values it holds. In certain conditions, values which are not
An implementation may impose an arbitrarily determined latency between the time a value contained in a WeakSet becomes inaccessible and the time when the value is removed from the WeakSet. If this latency was observable to ECMAScript program, it would be a source of indeterminacy that could impact program execution. For that reason, an ECMAScript implementation must not provide any means to determine if a WeakSet contains a particular value that does not require the observer to present the observed value.
WeakSets must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of WeakSets. It is not intended to be a viable implementation model.
See the NOTE in
24.4.1 The WeakSet Constructor
The WeakSet
- is %WeakSet%.
- is the initial value of the
"WeakSet" property of theglobal object . - creates and initializes a new WeakSet when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- may be used as the value in an
extends
clause of a class definition. Subclassconstructors that intend to inherit the specified WeakSet behaviour must include asuper
call to the WeakSetconstructor to create and initialize the subclass instance with the internal state necessary to support theWeakSet.prototype
built-in methods.
24.4.1.1 WeakSet ( [ iterable ] )
This function performs the following steps when called:
- If NewTarget is
undefined , throw aTypeError exception. - Let set be ?
OrdinaryCreateFromConstructor (NewTarget,"%WeakSet.prototype%" , « [[WeakSetData]] »). - Set set.[[WeakSetData]] to a new empty
List . - If iterable is either
undefined ornull , return set. - Let adder be ?
Get (set,"add" ). - If
IsCallable (adder) isfalse , throw aTypeError exception. - Let iteratorRecord be ?
GetIterator (iterable,sync ). - Repeat,
- Let next be ?
IteratorStepValue (iteratorRecord). - If next is
done , return set. - Let status be
Completion (Call (adder, set, « next »)). IfAbruptCloseIterator (status, iteratorRecord).
- Let next be ?
24.4.2 Properties of the WeakSet Constructor
The WeakSet
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
24.4.2.1 WeakSet.prototype
The initial value of WeakSet.prototype
is the
This property has the attributes { [[Writable]]:
24.4.3 Properties of the WeakSet Prototype Object
The WeakSet prototype object:
- is %WeakSet.prototype%.
- has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have a [[WeakSetData]] internal slot.
24.4.3.1 WeakSet.prototype.add ( value )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[WeakSetData]]). - If
CanBeHeldWeakly (value) isfalse , throw aTypeError exception. - For each element e of S.[[WeakSetData]], do
- If e is not
empty andSameValue (e, value) istrue , then- Return S.
- If e is not
- Append value to S.[[WeakSetData]].
- Return S.
24.4.3.2 WeakSet.prototype.constructor
The initial value of WeakSet.prototype.constructor
is
24.4.3.3 WeakSet.prototype.delete ( value )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[WeakSetData]]). - If
CanBeHeldWeakly (value) isfalse , returnfalse . - For each element e of S.[[WeakSetData]], do
- If e is not
empty andSameValue (e, value) istrue , then- Replace the element of S.[[WeakSetData]] whose value is e with an element whose value is
empty . - Return
true .
- Replace the element of S.[[WeakSetData]] whose value is e with an element whose value is
- If e is not
- Return
false .
The value
24.4.3.4 WeakSet.prototype.has ( value )
This method performs the following steps when called:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[WeakSetData]]). - If
CanBeHeldWeakly (value) isfalse , returnfalse . - For each element e of S.[[WeakSetData]], do
- If e is not
empty andSameValue (e, value) istrue , returntrue .
- If e is not
- Return
false .
24.4.3.5 WeakSet.prototype [ @@toStringTag ]
The initial value of the
This property has the attributes { [[Writable]]:
24.4.4 Properties of WeakSet Instances
WeakSet instances are