Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

WeakSet

BaselineWidely available *

AWeakSet is a collection of garbage-collectable values, including objects andnon-registered symbols. A value in theWeakSet may only occur once. It is unique in theWeakSet's collection.

Description

Values of WeakSets must be garbage-collectable. Mostprimitive data types can be arbitrarily created and don't have a lifetime, so they cannot be stored. Objects andnon-registered symbols can be stored because they are garbage-collectable.

The main differences to theSet object are:

  • WeakSets are collections ofobjects and symbols only. They cannot contain arbitrary values of any type, asSets can.

  • TheWeakSet isweak, meaning references to objects in aWeakSet are heldweakly. If no other references to a value stored in theWeakSet exist, those values can be garbage collected.

    Note:This also means that there is no list of current values stored in the collection.WeakSets are not enumerable.

Use case: Detecting circular references

Functions that call themselves recursively need a way of guarding against circular data structures by tracking which objects have already been processed.

WeakSets are ideal for this purpose:

js
// Execute a callback on everything stored inside an objectfunction execRecursively(fn, subject, _refs = new WeakSet()) {  // Avoid infinite recursion  if (_refs.has(subject)) {    return;  }  fn(subject);  if (typeof subject === "object" && subject) {    _refs.add(subject);    for (const key in subject) {      execRecursively(fn, subject[key], _refs);    }    _refs.delete(subject);  }}const foo = {  foo: "Foo",  bar: {    bar: "Bar",  },};foo.bar.baz = foo; // Circular reference!execRecursively((obj) => console.log(obj), foo);

Here, aWeakSet is created on the first run, and passed along with every subsequent function call (using the internal_refs parameter).

The number of objects or their traversal order is immaterial, so aWeakSet is more suitable (and performant) than aSet for tracking object references, especially if a very large number of objects is involved.

Constructor

WeakSet()

Creates a newWeakSet object.

Instance properties

These properties are defined onWeakSet.prototype and shared by allWeakSet instances.

WeakSet.prototype.constructor

The constructor function that created the instance object. ForWeakSet instances, the initial value is theWeakSet constructor.

WeakSet.prototype[Symbol.toStringTag]

The initial value of the[Symbol.toStringTag] property is the string"WeakSet". This property is used inObject.prototype.toString().

Instance methods

WeakSet.prototype.add()

Appendsvalue to theWeakSet object.

WeakSet.prototype.delete()

Removesvalue from theWeakSet.WeakSet.prototype.has(value) will returnfalse afterwards.

WeakSet.prototype.has()

Returns a boolean asserting whethervalue is present in theWeakSet object or not.

Examples

Using the WeakSet object

js
const ws = new WeakSet();const foo = {};const bar = {};ws.add(foo);ws.add(bar);ws.has(foo); // truews.has(bar); // truews.delete(foo); // removes foo from the setws.has(foo); // false, foo has been removedws.has(bar); // true, bar is retained

Note thatfoo !== bar. While they are similar objects,they are notthe same object. And so they are both added to the set.

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-weakset-objects

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp