Using WeakMap for Collections of Key-Value Pairs in Javascript


Create a collection of key-value pairs where keys are objects and do not prevent garbage collection.

Source Code

let obj1 = {};
let obj2 = {};
let weakMap = new WeakMap([[obj1, 'value1'], [obj2, 'value2']]);
console.log(weakMap.get(obj1)); // "value1"
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments