Peter Seliger
Dec 17, 2023

The provided example code obviously never got tested. One can not just throw random object literals as keys at a WeakMap instance in order to retrieve/access values. But one always has to use either references of objects of any kind or non-registered symbols as keys. Thus the example code at its very beginning has to be written like that ...

const john = { name: 'John' };
const jane = { name: 'Jane' };

const bob = Symbol('Bob');

… and only then one continues like follows …

const myMap = new WeakMap([
[john, 'John Doe'],
[jane, 'Jane Doe'],
]);

myMap.set(bob, 'Bob Doe');

console.log(myMap.has(jane)); // true

console.log(myMap.get(bob)); // 'Bob Doe'
console.log(myMap.get(john)); // 'John Doe'

console.log(myMap.delete(john)); // true
console.log(myMap.delete(john)); // false

No responses yet