-
Notifications
You must be signed in to change notification settings - Fork 1
/
weak-maps.js
34 lines (26 loc) · 1007 Bytes
/
weak-maps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/** Weak Maps **/
// We can think of weak maps as a sub set of Maps.
// WeakMaps are not iterable, i.e. there is no iterable protocol.
// Every key must be an object.
var map = new WeakMap()
map.set('foo', 'Mr.Foo') // TypeError: Invalid value used as weak map key
// This enables the map keys to be garage collected
// when they're only being referenced as WeakMap keys.
// This is useful when we want to store data that will
// eventually be lost, e.g. DOM nodes.
// We can pass an iterable to populate the WeakMap.
var foo = new Date()
var bar = () => {}
var map = new WeakMap([[foo, 'Mr.foo'], [bar, 'Mr.bar']]);
// .has()
console.log(map.has(foo)) // true
// .get()
console.log(map.get(bar)) // 'Mr.Bar'
// .delete()
map.delete(foo);
console.log(map.has(foo)) // false
// Why should we use it?
// The answer is garbage collection.
// Because the keys are weakly store, this means that if
// there are no other references to one of its keys,
// then the object is subject to garbage collection.