ECMAScript 6 introduces weak maps, available in Node.JS v0.11.3 with the --harmony
flag. Consider the following.
let weakMap = WeakMap();
let key = [];
let rubbish = 'fish cans';
weakMap.set(key, rubbish);
rubbish = 'empty bottle';
// Prints "fish cans"
console.log(weakMap.get(key));
I was under the impression that, for weak maps, the reference from the key to the value is weak, so that if the only reference to the value is the key, then the value can no longer be accessed.
Why then is the value 'fish cans'
still accessible and not garbage collected? The variable rubbish
no longer references it, and the reference from key
to 'fish cans'
is weak, i.e. non-existant from the point of view of the garbage collector. What am I missing?