WeakSet
) [ES6] (advanced)
WeakSets are similar to Sets, with the following differences:
They can hold objects without preventing those objects from being garbage-collected.
They are black boxes: we only get any data out of a WeakSet if we have both the WeakSet and a value. The only methods that are supported are .add()
, .delete()
, .has()
. Consult the section on WeakMaps as black boxes for an explanation of why WeakSets don’t allow iteration, looping, and clearing.
Given that we can’t iterate over their elements, there are not that many use cases for WeakSets. They do enable us to mark objects.
The following code demonstrates how a class can ensure that its methods are only applied to instances that were created by it (based on code by Domenic Denicola):
const instancesOfSafeClass = new WeakSet();
class SafeClass {
constructor() {
instancesOfSafeClass.add(this);
}
method() {
if (!instancesOfSafeClass.has(this)) {
throw new TypeError('Incompatible object!');
}
}
}
const safeInstance = new SafeClass();
safeInstance.method(); // works
assert.throws(
() => {
const obj = {};
SafeClass.prototype.method.call(obj); // throws an exception
},
TypeError
);
The constructor and the three methods of WeakSet
work the same as their Set
equivalents:
new WeakSet<T>(values?: Iterable<T>)
[ES6]
.add(value: T): this
[ES6]
.delete(value: T): boolean
[ES6]
.has(value: T): boolean
[ES6]