[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/WeakSet [Back]  [Original]

WeakSet - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

WeakSet

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

* Some parts of this feature may have varying levels of support.

WeakSet , , . WeakSet , WeakSet.

In this article

WeakSet . , . .

Set:

  • WeakSet . Set .

  • WeakSet , WeakSet . , WeakSet, .

    : , . WeakSet .

:

, , , .

WeakSet :

js
//  `fn`  ,    .
function execRecursively(fn, subject, _refs = new WeakSet()) {
  //   
  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; //  !
execRecursively((obj) => console.log(obj), foo);

WeakSet ( _refs).

, WeakSet , Set , .

WeakSet()

WeakSet.

WeakSet.prototype WeakSet.

WeakSet.prototype.constructor

-, . WeakSet WeakSet.

WeakSet.prototype[@@toStringTag]

@@toStringTag "WeakSet". Object.prototype.toString().

WeakSet.prototype.add()

value WeakSet.

WeakSet.prototype.delete()

value WeakSet. WeakSet.prototype.has(value) false.

WeakSet.prototype.has()

, value WeakSet .

WeakSet

js
const ws = new WeakSet();
const foo = {};
const bar = {};

ws.add(foo);
ws.add(bar);

ws.has(foo); // true
ws.has(bar); // true

ws.delete(foo); //  foo  
ws.has(foo); // false, foo  
ws.has(bar); // true, bar 

, foo !== bar. , . .

Specification
ECMAScript 2027 LanguageSpecification
# sec-weakset-objects


Web Proxy Viewer  |  New URL  |  Original Page