Skip to content

Commit 9e08049

Browse files
authored
👾 smth
1 parent 78a9566 commit 9e08049

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
12
# WeakMap and WeakSet
23

34
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.
45

56
For instance:
7+
68
```js
79
let john = { name: "John" };
810

@@ -54,13 +56,13 @@ john = null; // overwrite the reference
5456
*/!*
5557
```
5658

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

5961
Let's see what it means on examples.
6062

6163
## WeakMap
6264

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

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

9597
`WeakMap` has only the following methods:
9698

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

102104
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*.
103105

@@ -242,11 +244,11 @@ obj = null;
242244

243245
## WeakSet
244246

245-
[`WeakSet`](mdn:js/WeakSet) behaves similarly:
247+
[`WeakSet`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) behaves similarly:
246248

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

251253
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.
252254

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

281283
## Summary
282284

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

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

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

0 commit comments

Comments
 (0)