This is a new technology, part of the ECMAScript 2015 (ES6) standard.
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.
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,WeakSetssó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ó.WeakSetsno 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
WeakSetfunction by default.
Mètodes
WeakSet.prototype.add(value)- Appends a new element with the given value to the
WeakSetobject. WeakSet.prototype.delete(value)- Removes the element associated to the
value.WeakSet.prototype.has(value)will returnfalseafterwards. WeakSet.prototype.has(value)- Returns a boolean asserting whether an element is present with the given value in the
WeakSetobject or not. WeakSet.prototype.clear()Removes all elements from theWeakSetobject.
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
| Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Suport bàsic | 36 | 34 (34) | Not supported | 23 | 9 |
new WeakSet(iterable) |
38 | 34 (34) | Not supported | 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 | Not supported | 34.0 (34) | Not supported | Not supported | 9 |
new WeakMap(iterable) |
Not supported | 34.0 (34) | Not supported | Not supported | 9 |
Constructor argument: new WeakSet(null) |
? | (Yes) | ? | ? | 9 |
Monkey-patched add() in Constructor |
? | (Yes) | ? | ? | 9 |