How it works...

Membership to a WeakSet is evaluated similarly to the === operator. Looking at the  preceding, we can see that two Rocket instances with the name US: Saturn V are added to the set. This is, of course, because the set isn't comparing name properties, but object instances. Thus, we'll see two rather than one (unique names) or three (number of times added) US: Saturn V entries.

Next, you can see that the US: Falcon Heavy is removed after being initially added. Subsequently, the set doesn't have that member.

An error is thrown when trying to add a string to the WeakSet. Like the keys of WeakMap, the elements of WeakSet must be objects. This is so that the WeakSet can only keep weak references to its entries. Thus, the memory allocated ...

Get ECMAScript Cookbook 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.