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/07-map-set-weakmap-weakset/article.md
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -137,17 +137,17 @@ let recipeMap = new Map([
137
137
]);
138
138
139
139
// iterate over keys (vegetables)
140
-
for(let vegetable of recipeMap.keys()) {
140
+
for(let vegetable of recipeMap.keys()) {
141
141
alert(vegetable); // cucumber, tomateos, onion
142
142
}
143
143
144
144
// iterate over values (amounts)
145
-
for(let amount of recipeMap.values()) {
145
+
for(let amount of recipeMap.values()) {
146
146
alert(amount); // 500, 350, 50
147
147
}
148
148
149
149
// iterate over [key, value] entries
150
-
for(let entry of recipeMap) { // the same as of recipeMap.entries()
150
+
for(let entry of recipeMap) { // the same as of recipeMap.entries()
151
151
alert(entry); // cucumber,500 (and so on)
152
152
}
153
153
```
@@ -199,7 +199,7 @@ set.add(mary);
199
199
// set keeps only unique values
200
200
alert( set.size ); // 3
201
201
202
-
for(let user of set) {
202
+
for(let user of set) {
203
203
alert(user.name); // John (then Pete and Mary)
204
204
}
205
205
```
@@ -213,7 +213,7 @@ We can loop over a set either with `for..of` or using `forEach`:
213
213
```js run
214
214
let set = new Set(["oranges", "apples", "bananas"]);
215
215
216
-
for(let value of set) alert(value);
216
+
for(let value of set) alert(value);
217
217
218
218
// the same with forEach:
219
219
set.forEach((value, valueAgain, set) => {
@@ -332,7 +332,7 @@ That's useful for situations when we have a main storage for the objects somewhe
332
332
333
333
Let's see an example.
334
334
335
-
For instance, we have a code that keeps a visit count for each user. The information is stored in a map: a user is the key and the visit count is the value. When a user leaves, we don't want to store his visit count any more.
335
+
For instance, we have a code that keeps a visit count for each user. The information is stored in a map: a user is the key and the visit count is the value. When a user leaves, we don't want to store his visit count anymore.
336
336
337
337
One way would be to keep track of leaving users and clean up the storage manually:
338
338
@@ -345,7 +345,7 @@ let visitsCountMap = new Map();
345
345
// john is the key for the map
346
346
visitsCountMap.set(john, 123);
347
347
348
-
// now john leaves us, we don't need him any more
348
+
// now john leaves us, we don't need him anymore
349
349
john = null;
350
350
351
351
*!*
@@ -364,7 +364,7 @@ let visitsCountMap = new WeakMap();
364
364
365
365
visitsCountMap.set(john, 123);
366
366
367
-
// now john leaves us, we don't need him any more
367
+
// now john leaves us, we don't need him anymore
368
368
john = null;
369
369
370
370
// there are no references except WeakMap,
@@ -408,7 +408,7 @@ The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterati
408
408
409
409
## Summary
410
410
411
-
- `Map` -- is a a collection of keyed values.
411
+
- `Map` -- is a collection of keyed values.
412
412
413
413
The differences from a regular `Object`:
414
414
@@ -421,12 +421,12 @@ The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterati
421
421
- Unlike an array, does not allow to reorder elements.
422
422
- Keeps the insertion order.
423
423
424
-
- `WeakMap` -- a variant of `Map` that allows only objects as keys and removes them once they become unaccessible by other means.
424
+
- `WeakMap` -- a variant of `Map` that allows only objects as keys and removes them once they become inaccessible by other means.
425
425
426
426
- It does not support operations on the structure as a whole: no `size`, no `clear()`, no iterations.
427
427
428
-
- `WeakSet` -- is a variant of `Set` that only stores objects and removes them once they become unaccessible by other means.
428
+
- `WeakSet` -- is a variant of `Set` that only stores objects and removes them once they become inaccessible by other means.
429
429
430
430
- Also does not support `size/clear()` and iterations.
431
431
432
-
`WeakMap` and `WeakSet` are used as "secondary" data structures in additional to the "main" object storage. Once the object is removed from the main storage, so it only stays in `WeakMap/WeakSet`, they clean up aumatically.
432
+
`WeakMap` and `WeakSet` are used as "secondary" data structures in addition to the "main" object storage. Once the object is removed from the main storage, so it only stays in `WeakMap/WeakSet`, they clean up automatically.
0 commit comments