I have this simple feature based use case/Example for WeakMaps.

MANAGE A COLLECTION OF USERS

I started off with a User Object whose properties include a fullname , username , age , gender and a method called print which prints a human readable summary of the other properties.

/** Basic User Object with common properties. */ function User(username, fullname, age, gender) { this.username = username; this.fullname = fullname; this.age = age; this.gender = gender; this.print = () => console.log(`${this.fullname} is a ${age} year old ${gender}`); }

I then added a Map called users to keep a collection of multiple users which are keyed by username .

/** Collection of Users, keyed by username. */ var users = new Map();

Addition of the Collection also required helper functions to add, get, delete a User and even a function to print all the users for sake of completeness.

/** Creates an User Object and adds it to the users Collection. */ var addUser = (username, fullname, age, gender) => { let an_user = new User(username, fullname, age, gender); users.set(username, an_user); } /** Returns an User Object associated with the given username in the Collection. */ var getUser = (username) => { return users.get(username); } /** Deletes an User Object associated with the given username in the Collection. */ var deleteUser = (username) => { users.delete(username); } /** Prints summary of all the User Objects in the Collection. */ var printUsers = () => { users.forEach((user) => { user.print(); }); }

With all of the above code running in, say NodeJS, only the users Map has the reference to the User Objects within the entire process. There is no other reference to the individual User Objects.

Running this code an interactive NodeJS shell, just as an Example I add four users and print them:

ADD MORE INFO TO USERS WITHOUT MODIFYING EXISTING CODE

Now say a new feature is required wherein each users Social Media Platform (SMP) links need to be tracked along with the User Objects.

The key here is also that this feature must be implemented with minimum intervention to the existing code.

This is possible with WeakMaps in the following manner.

I add three separate WeakMaps for Twitter, Facebook, LinkedIn.

/* WeakMaps for Social Media Platforms (SMPs). Could be replaced by a single Map which can grow dynamically based on different SMP names . . . anyway... */ var sm_platform_twitter = new WeakMap(); var sm_platform_facebook = new WeakMap(); var sm_platform_linkedin = new WeakMap();

A helper function, getSMPWeakMap is added simply to return the WeakMap associated with the given SMP name.

/** Returns the WeakMap for the given SMP. */ var getSMPWeakMap = (sm_platform) => { if(sm_platform == "Twitter") { return sm_platform_twitter; } else if(sm_platform == "Facebook") { return sm_platform_facebook; } else if(sm_platform == "LinkedIn") { return sm_platform_linkedin; } return undefined; }

A function to add a users SMP link to the given SMP WeakMap.

/** Adds a SMP link associated with a given User. The User must be already added to the Collection. */ var addUserSocialMediaLink = (username, sm_platform, sm_link) => { let user = getUser(username); let sm_platform_weakmap = getSMPWeakMap(sm_platform); if(user && sm_platform_weakmap) { sm_platform_weakmap.set(user, sm_link); } }

A function to print only the users who are present on the given SMP.

/** Prints the User's fullname and corresponding SMP link of only those Users which are on the given SMP. */ var printSMPUsers = (sm_platform) => { let sm_platform_weakmap = getSMPWeakMap(sm_platform); console.log(`Users of ${sm_platform}:`) users.forEach((user)=>{ if(sm_platform_weakmap.has(user)) { console.log(`\t${user.fullname} : ${sm_platform_weakmap.get(user)}`) } }); }

You can now add SMP links for the users, also with the possibility of each user having a link on multiple SMPs.

...continuing with the earlier Example, I add SMP links to the users, multiple links for users Bill and Sarah and then print the links for each SMP separately:

Now say a User is deleted from the users Map by calling deleteUser . That removes the only reference to the User Object. This in turn will also clear out the SMP link from any/all of the SMP WeakMaps (by Garbage Collection) as without the User Object there is no way to access any of its SMP link.

...continuing with the Example, I delete user Bill and then print out the links of the SMPs he was associated with:

There is no requirement of any additional code to individually delete the SMP link separately and the existing code before this feature was not modified in anyway.

If there is any other way to add this feature with/without WeakMaps please feel free to comment.