--- title: WeakSet slug: Web/JavaScript/Reference/Global_Objects/WeakSet tags: - ECMAScript6 - Experimental - JavaScript - NeedsTranslation - TopicStub - WeakSet translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet ---
{{JSRef}}

L'objecte WeakSet permet emmagatzemar dèbilment objects en una col·lecció.

Sintaxi

 new WeakSet([iterable]);

Paràmetres

iterable
Si es pasa un objecte iterable, tots els seus elements seràn afegits al nou WeakSet. null es tractat com a undefined.

Descripció

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:

Propietats

WeakSet.length
El valor de la propietat length és 0.
{{jsxref("WeakSet.prototype")}}
Representa el prototip per al constructor de Set. Permet afegir propietats a tots els objectes WeakSet.

Instàncies WeakSet

Totes les instàncies WeakSet hereten de {{jsxref("WeakSet.prototype")}}.

Propietats

{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Properties')}}

Mètodes

{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Methods')}}

Exemples

Utilitzar l'objecte WeakSet

var 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

Especificacions

Especificació Estat Comentaris
{{SpecName('ES6', '#sec-weakset-objects', 'WeakSet')}} {{Spec2('ES6')}} Definició inicial.

Compatibilitat amb navegadors

{{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

Vegeu també