Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
WeakMap and WeakSet#158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
52 changes: 26 additions & 26 deletions1-js/05-data-types/08-weakmap-weakset/01-recipients-read/solution.md
This file contains hidden or 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 @@ | ||
Uložme přečtené zprávy do `WeakSet`: | ||
```js run | ||
letzprávy = [ | ||
{text: "Ahoj",od: "Jan"}, | ||
{text: "Jak se máš?",od: "Jan"}, | ||
{text: "Brzy nashle",od: "Alice"} | ||
]; | ||
letpřečtenéZprávy = new WeakSet(); | ||
//dvě zprávy byly přečteny | ||
přečtenéZprávy.add(zprávy[0]); | ||
přečtenéZprávy.add(zprávy[1]); | ||
//přečtenéZprávy obsahuje 2prvky | ||
// ...znovu přečteme první zprávu! | ||
přečtenéZprávy.add(zprávy[0]); | ||
//přečtenéZprávy stále obsahuje 2unikátní prvky | ||
//odpověď: byla zpráva s indexem 0 přečtena? | ||
alert("Zpráva 0 přečtena: " +přečtenéZprávy.has(zprávy[0])); // true | ||
zprávy.shift(); | ||
//nyní přečtenéZprávy obsahuje 1prvek (technicky může být paměť vyčištěna později) | ||
``` | ||
`WeakSet`nám umožňuje uložit množinu zpráv a snadno ověřovat, zda v ní zpráva existuje. | ||
Automaticky se vyčistí. Nevýhodou je, že nad ní nemůžeme iterovat, nemůžeme získat „všechny přečtené zprávy“ přímo z ní. Můžeme to však udělat iterací nad všemi zprávami a filtrováním těch, které jsou v této množině. | ||
Jiným řešením by bylo přidání vlastnosti, např. `zpráva.jePřečtena=true`, do zprávy poté, co bude přečtena. Pokud objekty zpráv spravuje jiný kód, obecně se to nedoporučuje, ale můžeme se vyhnout konfliktům použitím symbolické vlastnosti. | ||
Třeba takto: | ||
```js | ||
//symbolickou vlastnost zná pouze náš kód | ||
letjePřečtena = Symbol("jePřečtena"); | ||
zprávy[0][jePřečtena] = true; | ||
``` | ||
Nyní kód třetí strany naši přidanou vlastnost neuvidí. | ||
Přestože symboly mohou snížit pravděpodobnost problémů, z architektonického hlediska je lepší použít `WeakSet`. |
20 changes: 10 additions & 10 deletions1-js/05-data-types/08-weakmap-weakset/01-recipients-read/task.md
This file contains hidden or 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
16 changes: 8 additions & 8 deletions1-js/05-data-types/08-weakmap-weakset/02-recipients-when-read/solution.md
This file contains hidden or 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,15 @@ | ||
K uložení data můžeme použít `WeakMap`: | ||
```js | ||
letzprávy = [ | ||
{text: "Ahoj",od: "Jan"}, | ||
{text: "Jak se máš?",od: "Jan"}, | ||
{text: "Brzy se uvidíme",od: "Alice"} | ||
]; | ||
letmapaPřečtení = new WeakMap(); | ||
mapaPřečtení.set(zprávy[0], new Date(2017, 1, 1)); | ||
//objektyDateprostudujeme později | ||
``` |
18 changes: 9 additions & 9 deletions1-js/05-data-types/08-weakmap-weakset/02-recipients-when-read/task.md
This file contains hidden or 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.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.