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 Set
són:
- Al contrari que
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 enWeakSet
, 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.
Propietats
WeakSet.length
- El valor de la propietat
length
és 0. WeakSet.prototype
- Representa el prototip per al constructor de
Set.
Permet afegir propietats a tots els objectesWeakSet
.
Instàncies WeakSet
Totes les instàncies WeakSet
hereten de WeakSet.prototype
.
Propietats
WeakSet.prototype.constructor
- Returns the function that created an instance's prototype. This is the
WeakSet
function by default.
Mètodes
WeakSet.prototype.add(value)
- Appends a new object with the given value to the
WeakSet
object. WeakSet.prototype.delete(value)
- Removes the element associated to the
value
.WeakSet.prototype.has(value)
will returnfalse
afterwards. WeakSet.prototype.has(value)
- Returns a boolean asserting whether an element is present with the given value in the
WeakSet
object or not. WeakSet.prototype.clear()
Removes all elements from theWeakSet
object.
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 |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'WeakSet' in that specification. |
Standard | Definició inicial. |
Compatibilitat amb navegadors
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Suport bàsic | 36 | 34 (34) | No support | 23 | 9 |
new WeakSet(iterable) |
38 | 34 (34) | No support | 25 | 9 |
Constructor argument: new WeakSet(null) |
(Yes) | 37 (37) | ? | ? | 9 |
Monkey-patched add() in Constructor |
(Yes) | 37 (37) | ? | ? | 9 |
Característica | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Suport bàsic | No support | 34.0 (34) | No support | No support | 9 |
new WeakMap(iterable) |
No support | 34.0 (34) | No support | No support | 9 |
Constructor argument: new WeakSet(null) |
? | (Yes) | ? | ? | 9 |
Monkey-patched add() in Constructor |
? | (Yes) | ? | ? | 9 |