Skip to content

Commit 530dc9f

Browse files
authored
👾 smth
1 parent af4843b commit 530dc9f

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

‎1-js/05-data-types/08-weakmap-weakset/article.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ john = null; // overwrite the reference
5454
*/!*
5555
```
5656

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.
5858

5959
Let's see what it means on examples.
6060

6161
## WeakMap
6262

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:
6464

6565
```js run
6666
let weakMap = new WeakMap();
@@ -94,10 +94,10 @@ Compare it with the regular `Map` example above. Now if `john` only exists as th
9494

9595
`WeakMap` has only the following methods:
9696

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)
101101

102102
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*.
103103

@@ -242,11 +242,11 @@ obj = null;
242242

243243
## WeakSet
244244

245-
`WeakSet` behaves similarly:
245+
[`WeakSet`](mdn:js/WeakSet) behaves similarly:
246246

247247
- It is analogous to `Set`, but we may only add objects to `WeakSet` (not primitives).
248248
- 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.
250250

251251
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.
252252

@@ -280,9 +280,9 @@ The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterati
280280

281281
## Summary
282282

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.
284284

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.
286286

287287
Their main advantages are that they have weak reference to objects, so they can easily be removed by garbage collector.
288288

0 commit comments

Comments
 (0)