WeakSet

Here are the differences between Set and WeakSet objects:

  • A Set can store primitive types and object references whereas a WeakSet object can only store object references
  • One important features WeakSet objects is that if there is no other reference to an object stored in a WeakSet object then they are garbage-collected
  • Lastly, a WeakSet object is not enumerable: that is, you cannot find its size; it also doesn't implement the iterable protocol

Apart from these three differences, WeakSet behaves exactly the same way as Set. Everything else apart from these three differences is same between a Set and WeakSet object.

A WeakSet object is created using the WeakSet constructor. You cannot pass an iterable object as an argument to a WeakSet ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.