- Hands-On Data Structures and Algorithms with JavaScript
- Kashyap Mukkamala
- 138字
- 2025-02-25 19:09:24
The API difference
The API difference as documented in WeakMap case of maps is pretty close to what you can find for Sets as well:
- Set.prototype.size: Returns the size of the set
- Set.prototype.add: Adds a value for a given element and returns the entire new set
- Set.prototype.delete: Deletes an element and returns true if delete was successful, otherwise false
- Set.prototype.has: Checks the set for the presence of an element and returns a Boolean
- Set.prototype.clear: Clears the set and returns nothing
- Set.prototype.forEach: Loops over the set and gives access to each element
- Set.prototype.values: Returns an iterator, which can be used to get access to the next value
- Set.prototype.keys: Similar to values—returns access to values in the set
WeakSet, on the other hand, does not contain the forEach(), keys(), and values() methods for reasons discussed previously.