|
| 1 | + |
1 | 2 | # WeakMap and WeakSet
|
2 | 3 |
|
3 | 4 | As we know from the chapter <info:garbage-collection>, JavaScript engine keeps a value in memory while it is "reachable" and can potentially be used.
|
4 | 5 |
|
5 | 6 | For instance:
|
| 7 | + |
6 | 8 | ```js
|
7 | 9 | let john = { name: "John" };
|
8 | 10 |
|
@@ -54,13 +56,13 @@ john = null; // overwrite the reference
|
54 | 56 | */!*
|
55 | 57 | ```
|
56 | 58 |
|
57 |
| -[`WeakMap`](mdn:js/WeakMap) is fundamentally different in this aspect. It doesn't prevent garbage-collection of key objects. |
| 59 | +[`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) is fundamentally different in this aspect. It doesn't prevent garbage-collection of key objects. |
58 | 60 |
|
59 | 61 | Let's see what it means on examples.
|
60 | 62 |
|
61 | 63 | ## WeakMap
|
62 | 64 |
|
63 |
| -The first difference between [`Map`](mdn:js/Map) and [`WeakMap`](mdn:js/WeakMap) is that keys must be objects, not primitive values: |
| 65 | +The first difference between [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) is that keys must be objects, not primitive values: |
64 | 66 |
|
65 | 67 | ```js run
|
66 | 68 | let weakMap = new WeakMap();
|
@@ -94,10 +96,10 @@ Compare it with the regular `Map` example above. Now if `john` only exists as th
|
94 | 96 |
|
95 | 97 | `WeakMap` has only the following methods:
|
96 | 98 |
|
97 |
| -- [`weakMap.set(key, value)`](mdn:js/WeakMap/set) |
98 |
| -- [`weakMap.get(key)`](mdn:js/WeakMap/get) |
99 |
| -- [`weakMap.delete(key)`](mdn:js/WeakMap/delete) |
100 |
| -- [`weakMap.has(key)`](mdn:js/WeakMap/has) |
| 99 | +- [`weakMap.set(key, value)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/set) |
| 100 | +- [`weakMap.get(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/get) |
| 101 | +- [`weakMap.delete(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/delete) |
| 102 | +- [`weakMap.has(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap/has) |
101 | 103 |
|
102 | 104 | Why such a limitation? That's for technical reasons. If an object has lost all other references (like `john` in the code above), then it is to be garbage-collected automatically. But technically it's not exactly specified *when the cleanup happens*.
|
103 | 105 |
|
@@ -242,11 +244,11 @@ obj = null;
|
242 | 244 |
|
243 | 245 | ## WeakSet
|
244 | 246 |
|
245 |
| -[`WeakSet`](mdn:js/WeakSet) behaves similarly: |
| 247 | +[`WeakSet`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) behaves similarly: |
246 | 248 |
|
247 | 249 | - It is analogous to `Set`, but we may only add objects to `WeakSet` (not primitives).
|
248 | 250 | - An object exists in the set while it is reachable from somewhere else.
|
249 |
| -- Like `Set`, it supports [`add`](mdn:js/Weakset/add), [`has`](mdn:js/Weakset/has) and [`delete`](mdn:js/Weakset/delete), but not `size`, `keys()` and no iterations. |
| 251 | +- Like `Set`, it supports [`add`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Weakset/add), [`has`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Weakset/has) and [`delete`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Weakset/delete), but not `size`, `keys()` and no iterations. |
250 | 252 |
|
251 | 253 | Being "weak", it also serves as additional storage. But not for arbitrary data, rather for "yes/no" facts. A membership in `WeakSet` may mean something about the object.
|
252 | 254 |
|
@@ -280,9 +282,9 @@ The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterati
|
280 | 282 |
|
281 | 283 | ## Summary
|
282 | 284 |
|
283 |
| -[`WeakMap`](mdn:js/WeakMap) is `Map`-like collection that allows only objects as keys and removes them together with associated value once they become inaccessible by other means. |
| 285 | +[`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) is `Map`-like collection that allows only objects as keys and removes them together with associated value once they become inaccessible by other means. |
284 | 286 |
|
285 |
| -[`WeakSet`](mdn:js/WeakSet) is `Set`-like collection that stores only objects and removes them once they become inaccessible by other means. |
| 287 | +[`WeakSet`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) is `Set`-like collection that stores only objects and removes them once they become inaccessible by other means. |
286 | 288 |
|
287 | 289 | Their main advantages are that they have weak reference to objects, so they can easily be removed by garbage collector.
|
288 | 290 |
|
|
0 commit comments