WeakSet
WeakSet is a special type of object, used to store data uniquely, without duplicates – similar to Set. However, WeakSet can only contain object key.
Creating a WeakSet
Syntax:
- The iterable parameter is an iterable object (optional), with each element being an object.
- If no parameters are passed to the constructor, the WeakSet will be empty.
Ex:
1 2 3 4 5 6 7 8 9 | // initialize an empty WeakSet let ws1 = new WeakSet(); // OK // initialize the WeakSet with an iterable object where each element is an object let obj1 = { x: 1 }; let ws2 = new WeakSet([obj1]); // OK // initialize the WeakSet with an iterable object but each element is not an object let ws3 = new WeakSet([1, "2"]); // Error: Invalid value used in weak set |
Methods
weakSet.add(value) : Add value to WeakSet and return the WeakSet itself.
Ex:
1 2 3 4 5 6 7 | let obj1 = { x: 1 }; let obj2 = { x: 2 }; let ws = new WeakSet(); ws.add(obj1).add(obj2).add(obj1); // OK // Add a value that is not an object to the set ws.add(1); // Error: Invalid value used in weak set |
weakSet.has(value) : Returns true if value exists in WeakSet, otherwise returns false.
Ex:
1 2 3 4 5 6 7 | let obj1 = { x: 1 }; let obj2 = { x: 2 }; let ws = new WeakSet(); ws.add(obj1).add(obj2).add(obj1); console.log(ws.has(obj1)); // trueconsole.log(ws.has(obj2)); // trueconsole.log(ws.has({ x: 1 })); // false |
weakSet.delete(value) : Removes value from WeakSet and returns true if that value exists, otherwise returns false.
Ex:
1 2 3 4 5 6 7 | let obj1 = { x: 1 }; let obj2 = { x: 2 }; let ws = new WeakSet(); ws.add(obj1).add(obj2).add(obj1); console.log(ws.delete(obj1)); // trueconsole.log(ws.delete(obj2)); // trueconsole.log(ws.delete({ x: 1 })); // falseconsole.log(ws.delete(obj1)); // false |
WeakMap
WeakMap is a structure that allows storing data in key-value style similar to Map, Object.
Creating a WeakSet
Syntax:
- The iterable parameter is an iterable object (optional) with each element being a two-dimensional array of the form [key, value].
- Properties in WeakMap key can only be objects.
Ex:
1 2 3 4 5 | let obj = { x: 1 }; let weakMap = new WeakMap(); weakMap.set(obj, 1); // OK - key is object weakMap.set("a", 1); // Error: Invalid value used as weak map key - key is string |
Methods
weakMap.set(key, value) : Used to store the value by the key attribute and return the WeakMap itself. In particular, the key must be an object, otherwise an error is reported.
Ex:
1 2 3 4 5 | let obj1 = { x: 1 }; let obj2 = { x: 2 }; let weakMap = new WeakMap(); weakMap.set(obj1, 1).set(obj2, 2); |
weakMap.get(key) : Returns the value of the key property. If the key does not exist, returns undefined.
Ex:
1 2 3 4 5 6 7 | let obj1 = { x: 1 }; let obj2 = { x: 2 }; let weakMap = new WeakMap(); weakMap.set(obj1, 1).set(obj2, 2); console.log(weakMap.get(obj1)); // 1console.log(weakMap.get({ x: 1 })); // undefined - vì {x:1} !== {x:1} |
weakMap.delete(key) : Delete the key attribute in WeakMap, and return true if the key exists, otherwise return false.
Ex:
1 2 3 4 5 6 7 | let obj1 = { x: 1 }; let obj2 = { x: 2 }; let weakMap = new WeakMap(); weakMap.set(obj1, 1).set(obj2, 2); console.log(weakMap.delete(obj1)); // trueconsole.log(weakMap.delete({ x: 1 })); // false - vì {x:1} !== {x:1} |
weakMap.has(key) : Returns true if the key exists in WeakMap, otherwise returns false.
Ex:
1 2 3 4 5 6 7 | let obj1 = { x: 1 }; let obj2 = { x: 2 }; let weakMap = new WeakMap(); weakMap.set(obj1, 1).set(obj2, 2); console.log(weakMap.has(obj1)); // trueconsole.log(weakMap.has({ x: 1 })); // false - vì {x:1} !== {x:1} |