You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/08-weakmap-weakset/article.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,13 +54,13 @@ john = null; // overwrite the reference
54
54
*/!*
55
55
```
56
56
57
-
`WeakMap` is fundamentally different in this aspect. It doesn't prevent garbage-collection of key objects.
57
+
[`WeakMap`](mdn:js/WeakMap) is fundamentally different in this aspect. It doesn't prevent garbage-collection of key objects.
58
58
59
59
Let's see what it means on examples.
60
60
61
61
## WeakMap
62
62
63
-
The first difference between `Map` and `WeakMap` is that keys must be objects, not primitive values:
63
+
The first difference between [`Map`](mdn:js/Map) and [`WeakMap`](mdn:js/WeakMap) is that keys must be objects, not primitive values:
64
64
65
65
```js run
66
66
let weakMap =newWeakMap();
@@ -94,10 +94,10 @@ Compare it with the regular `Map` example above. Now if `john` only exists as th
94
94
95
95
`WeakMap` has only the following methods:
96
96
97
-
-`weakMap.get(key)`
98
-
-`weakMap.set(key, value)`
99
-
-`weakMap.delete(key)`
100
-
-`weakMap.has(key)`
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)
101
101
102
102
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
103
@@ -242,11 +242,11 @@ obj = null;
242
242
243
243
## WeakSet
244
244
245
-
`WeakSet` behaves similarly:
245
+
[`WeakSet`](mdn:js/WeakSet) behaves similarly:
246
246
247
247
- It is analogous to `Set`, but we may only add objects to `WeakSet` (not primitives).
248
248
- An object exists in the set while it is reachable from somewhere else.
249
-
- Like `Set`, it supports `add`, `has` and `delete`, but not `size`, `keys()` and no iterations.
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.
250
250
251
251
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
252
@@ -280,9 +280,9 @@ The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterati
280
280
281
281
## Summary
282
282
283
-
`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.
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.
284
284
285
-
`WeakSet` is `Set`-like collection that stores only objects and removes them once they become inaccessible by other means.
285
+
[`WeakSet`](mdn:js/WeakSet) is `Set`-like collection that stores only objects and removes them once they become inaccessible by other means.
286
286
287
287
Their main advantages are that they have weak reference to objects, so they can easily be removed by garbage collector.
0 commit comments