--- title: WeakSet slug: Web/JavaScript/Reference/Global_Objects/WeakSet tags: - ECMAScript6 - Experimental - JavaScript - NeedsTranslation - TopicStub - WeakSet translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet ---
L'objecte WeakSet permet emmagatzemar dèbilment objects en una col·lecció.
new WeakSet([iterable]);
WeakSet. null es tractat com a undefined.Els objectes WeakSet són col·leccions d'objectes. Un objecte al WeakSet només pot passar un cop, és únic en la col·lecció de WeakSet.
Les principals diferències respecte l'objecte {{jsxref("Set")}} són:
Sets, WeakSets són únicament col·leccions d'objectes i no de valors arbitraris de qualsevol tipus.WeakSet és dèbil (weak): Les referències a la col·lecció es mantenen dèbilment. SI no hi ha cap altra referència a un objecte emmagatzemat en WeakSet, poden ser recollits com a brossa. Això també vol dir que no hi ha cap llista d'objectes actuals emmagatzemats a la col·lecció. WeakSets no són enumerables.WeakSet.lengthlength és 0.Set. Permet afegir propietats a tots els objectes WeakSet.WeakSetTotes les instàncies WeakSet hereten de {{jsxref("WeakSet.prototype")}}.
{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Properties')}}
{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Methods')}}
WeakSetvar ws = new WeakSet();
var obj = {};
var foo = {};
ws.add(window);
ws.add(obj);
ws.has(window); // true
ws.has(foo); // false, foo no s'ha afegit al conjunt
ws.delete(window); // elimina window del conjunt
ws.has(window); // false, window ha sigut eliminat
ws.clear(); // buida tot el WeakSet
| Especificació | Estat | Comentaris |
|---|---|---|
| {{SpecName('ES6', '#sec-weakset-objects', 'WeakSet')}} | {{Spec2('ES6')}} | Definició inicial. |
{{CompatibilityTable}}
| Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Suport bàsic | {{CompatChrome(36)}} | {{ CompatGeckoDesktop(34) }} | {{CompatNo}} | {{ CompatOpera(23) }} | 9 |
new WeakSet(iterable) |
38 | {{ CompatGeckoDesktop(34) }} | {{CompatNo}} | 25 | 9 |
Constructor argument: new WeakSet(null) |
{{CompatVersionUnknown}} | {{CompatGeckoDesktop("37")}} | {{CompatUnknown}} | {{CompatUnknown}} | 9 |
Monkey-patched add() in Constructor |
{{CompatVersionUnknown}} | {{CompatGeckoDesktop("37")}} | {{CompatUnknown}} | {{CompatUnknown}} | 9 |
| Característica | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Suport bàsic | {{CompatNo}} | {{ CompatGeckoMobile(34) }} | {{CompatNo}} | {{CompatNo}} | 9 |
new WeakMap(iterable) |
{{CompatNo}} | {{ CompatGeckoMobile(34) }} | {{CompatNo}} | {{CompatNo}} | 9 |
Constructor argument: new WeakSet(null) |
{{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | 9 |
Monkey-patched add() in Constructor |
{{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | 9 |