24.4 WeakSet Objects
WeakSet objects are collections of objects. A distinct object may only occur once as an element of a WeakSet's collection. A WeakSet may be queried to see if it contains a specific object, but no mechanism is provided for enumerating the objects it holds. In certain conditions, objects which are not
An implementation may impose an arbitrarily determined latency between the time an object contained in a WeakSet becomes inaccessible and the time when the object 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 object that does not require the observer to present the observed object.
WeakSet objects 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 WeakSet objects specification is only intended to describe the required observable semantics of WeakSet objects. 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 object when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- is designed to be subclassable. It may be used as the value in an
extends
clause of a class definition. Subclass constructors 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 ] )
When the WeakSet
function is called with optional argument iterable, the following steps are taken:
- 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). - Repeat,
- Let next be ?
IteratorStep (iteratorRecord). - If next is
false , return set. - Let nextValue be ?
IteratorValue (next). - Let status be
Call (adder, set, « nextValue »). - If status is an
abrupt completion , return ?IteratorClose (iteratorRecord, status).
- 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 )
The following steps are taken:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[WeakSetData]]). - If
Type (value) is not Object, throw aTypeError exception. - Let entries be the
List that is S.[[WeakSetData]]. - For each element e of entries, do
- If e is not
empty andSameValue (e, value) istrue , then- Return S.
- If e is not
- Append value as the last element of entries.
- Return S.
24.4.3.2 WeakSet.prototype.constructor
The initial value of WeakSet.prototype.constructor
is the
24.4.3.3 WeakSet.prototype.delete ( value )
The following steps are taken:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[WeakSetData]]). - If
Type (value) is not Object, returnfalse . - Let entries be the
List that is S.[[WeakSetData]]. - For each element e of entries, do
- If e is not
empty andSameValue (e, value) istrue , then- Replace the element of entries whose value is e with an element whose value is
empty . - Return
true .
- Replace the element of entries 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 )
The following steps are taken:
- Let S be the
this value. - Perform ?
RequireInternalSlot (S, [[WeakSetData]]). - Let entries be the
List that is S.[[WeakSetData]]. - If
Type (value) is not Object, returnfalse . - For each element e of entries, 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 ordinary objects that inherit properties from the WeakSet prototype. WeakSet instances also have a [[WeakSetData]] internal slot.