First of all weakMap.set({}, obj);
and weakMap.get({});
will never refer to the same element in the WeakMap because they are separate, anonymous object literals. In order for the WeakMap to be useful you need to store a reference to the object to be used as key1.
let weakMap = new WeakMap();
let obj = {name: 'Ivan'};
weakMap.set({}, obj);
console.log('has anonymous key:', weakMap.has({}));
console.log(weakMap.get({}));
let key = {};
weakMap.set(key, obj);
console.log('has assigned key:', weakMap.has(key));
console.log(weakMap.get(key));
Secondly, it is the key that is weakly referenced in the WeakMap, not the value, so setting the value to null
will have no effect on the WeakMap. Instead you need to 'null' the key2 (another reason you need a stored reference to it).
let weakMap = new WeakMap();
let obj = { name: 'Ivan' };
let key = {};
weakMap.set(key, obj);
console.log(weakMap);
key = null;
// no good way to indicate this, but debugging in the console will reveal an empty WeakMap pending garbage collection
console.log(weakMap);
1 Your example using an unreachable object as key will disappear from the WeakMap on garbage collection, but not because you set obj=null
but because the only reference to the object exists in the WeakMap.
2 There is no good way to demonstrate the behaviour of the second snippet because, per the documentation, '... a WeakMap doesn't allow observing the liveness of its keys, its keys are not enumerable.' Nonetheless, debugging in the console will reveal the behaviour, though awkwardly given the variety of ways different browsers treat objects in the console, ie. chrome's live objects.