-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from behrends/WeakMap_and_WeakSet
WeakMap and WeakSet
- Loading branch information
Showing
5 changed files
with
136 additions
and
138 deletions.
There are no files selected for viewing
36 changes: 18 additions & 18 deletions
36
1-js/05-data-types/08-weakmap-weakset/01-recipients-read/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
Let's store read messages in `WeakSet`: | ||
Lass uns gelesene Nachrichten in `WeakSet` speichern: | ||
|
||
```js run | ||
let messages = [ | ||
{text: "Hello", from: "John"}, | ||
{text: "How goes?", from: "John"}, | ||
{text: "See you soon", from: "Alice"} | ||
{text: "Hallo", from: "John"}, | ||
{text: "Wie läuft's?", from: "John"}, | ||
{text: "Bis bald", from: "Alice"} | ||
]; | ||
|
||
let readMessages = new WeakSet(); | ||
|
||
// two messages have been read | ||
// Zwei Nachrichten wurden gelesen | ||
readMessages.add(messages[0]); | ||
readMessages.add(messages[1]); | ||
// readMessages has 2 elements | ||
// readMessages hat 2 Elemente | ||
|
||
// ...let's read the first message again! | ||
// ...lass uns die erste Nachricht nochmal lesen! | ||
readMessages.add(messages[0]); | ||
// readMessages still has 2 unique elements | ||
// readMessages hat immer noch 2 einzigartige Elemente | ||
|
||
// answer: was the message[0] read? | ||
alert("Read message 0: " + readMessages.has(messages[0])); // true | ||
// Antwort: Wurde die Nachricht[0] gelesen? | ||
alert("Gelesene Nachricht 0: " + readMessages.has(messages[0])); // true | ||
|
||
messages.shift(); | ||
// now readMessages has 1 element (technically memory may be cleaned later) | ||
// jetzt hat readMessages 1 Element (technisch gesehen, könnte der Speicher später bereinigt werden) | ||
``` | ||
|
||
The `WeakSet` allows to store a set of messages and easily check for the existence of a message in it. | ||
Das `WeakSet` ermöglicht es, eine Menge von Nachrichten zu speichern und einfach zu überprüfen, ob eine Nachricht darin existiert. | ||
|
||
It cleans up itself automatically. The tradeoff is that we can't iterate over it, can't get "all read messages" from it directly. But we can do it by iterating over all messages and filtering those that are in the set. | ||
Es bereinigt sich automatisch. Der Kompromiss ist, dass wir nicht darüber iterieren können, d.h. wir können nicht "alle gelesenen Nachrichten" direkt erhalten. Aber wir können dies erreichen, indem wir über alle Nachrichten iterieren und diejenigen aussortieren, die nicht im Set sind. | ||
|
||
Another, different solution could be to add a property like `message.isRead=true` to a message after it's read. As messages objects are managed by another code, that's generally discouraged, but we can use a symbolic property to avoid conflicts. | ||
Eine andere, unterschiedliche Lösung könnte sein, einer Nachricht eine Eigenschaft wie `message.isRead=true` hinzuzufügen, nachdem sie gelesen wurde. Da Nachrichtenobjekte von anderem Code verwaltet werden, ist das allgemein nicht empfohlen, aber wir können eine symbolische Eigenschaft verwenden, um Konflikte zu vermeiden. | ||
|
||
Like this: | ||
So wie hier: | ||
```js | ||
// the symbolic property is only known to our code | ||
// die symbolische Eigenschaft ist nur unserem Code bekannt | ||
let isRead = Symbol("isRead"); | ||
messages[0][isRead] = true; | ||
``` | ||
|
||
Now third-party code probably won't see our extra property. | ||
Jetzt wird Code von dritten unsere zusätzliche Eigenschaft wahrscheinlich nicht sehen. | ||
|
||
Although symbols allow to lower the probability of problems, using `WeakSet` is better from the architectural point of view. | ||
Obwohl Symbole die Wahrscheinlichkeit von Problemen verringern, ist die Verwendung von `WeakSet` aus architektonischer Sicht besser. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
1-js/05-data-types/08-weakmap-weakset/02-recipients-when-read/solution.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
|
||
To store a date, we can use `WeakMap`: | ||
Um ein Datum zu speichern, können wir `WeakMap` verwenden: | ||
|
||
```js | ||
let messages = [ | ||
{text: "Hello", from: "John"}, | ||
{text: "How goes?", from: "John"}, | ||
{text: "See you soon", from: "Alice"} | ||
{text: "Hallo", from: "John"}, | ||
{text: "Wie läuft's?", from: "John"}, | ||
{text: "Bis bald", from: "Alice"} | ||
]; | ||
|
||
let readMap = new WeakMap(); | ||
|
||
readMap.set(messages[0], new Date(2017, 1, 1)); | ||
// Date object we'll study later | ||
// Date-Objekt, das wir später betrachten werden | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.