Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Proxy and Reflect#206
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
Show all changes
2 commits Select commitHold shift + click to select a range
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
22 changes: 11 additions & 11 deletions1-js/99-js-misc/01-proxy/01-error-nonexisting/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,23 +1,23 @@ | ||
```js run | ||
letuživatel = { | ||
jméno: "Jan" | ||
}; | ||
functionobal(cíl) { | ||
return new Proxy(cíl, { | ||
get(cíl, vlastnost, příjemce) { | ||
if (vlastnost incíl) { | ||
return Reflect.get(cíl, vlastnost, příjemce); | ||
} else { | ||
throw new ReferenceError(`Vlastnost neexistuje: "${vlastnost}"`) | ||
} | ||
} | ||
}); | ||
} | ||
uživatel =obal(uživatel); | ||
alert(uživatel.jméno); //Jan | ||
alert(uživatel.věk); // ReferenceError:Vlastnost neexistuje: "věk" | ||
``` |
28 changes: 14 additions & 14 deletions1-js/99-js-misc/01-proxy/01-error-nonexisting/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
#Chyba při načítání neexistující vlastnosti | ||
Pokus o načtení neexistující vlastnosti zpravidla vrátí `undefined`. | ||
Vytvořteproxy, která při pokusu o načtení neexistující vlastnosti místo toho vyvolá chybu. | ||
To nám může pomoci dříve detekovat programátorské chyby. | ||
Napište funkci `obal(cíl)`, která vezme objekt `cíl` a vrátíproxy, která přidá tento funkcionální aspekt. | ||
Mělo by to fungovat takto: | ||
```js | ||
letuživatel = { | ||
jméno: "Jan" | ||
}; | ||
functionobal(cíl) { | ||
return new Proxy(cíl, { | ||
*!* | ||
/*váš kód */ | ||
*/!* | ||
}); | ||
} | ||
uživatel =obal(uživatel); | ||
alert(uživatel.jméno); //Jan | ||
*!* | ||
alert(uživatel.věk); // ReferenceError:Vlastnost neexistuje: "věk" | ||
*/!* | ||
``` |
20 changes: 10 additions & 10 deletions1-js/99-js-misc/01-proxy/02-array-negative/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,19 +1,19 @@ | ||
```js run | ||
letpole = [1, 2, 3]; | ||
pole = new Proxy(pole, { | ||
get(cíl, vlastnost, příjemce) { | ||
if (vlastnost < 0) { | ||
//i když k poli přistupujeme přes pole[1], | ||
//vlastnost je řetězec, takže jej musíme konvertovat na číslo | ||
vlastnost = +vlastnost +cíl.length; | ||
} | ||
return Reflect.get(cíl, vlastnost, příjemce); | ||
} | ||
}); | ||
alert(pole[-1]); // 3 | ||
alert(pole[-2]); // 2 | ||
``` |
32 changes: 16 additions & 16 deletions1-js/99-js-misc/01-proxy/02-array-negative/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
#Přístup k poli[-1] | ||
V některých programovacích jazycích můžeme přistupovat k prvkům pole pomocí záporných indexů, které se počítají od konce. | ||
Například: | ||
```js | ||
letpole = [1, 2, 3]; | ||
pole[-1]; // 3,poslední prvek | ||
pole[-2]; // 2,jeden krok od konce | ||
pole[-3]; // 1,dva kroky od konce | ||
``` | ||
Jinými slovy, `pole[-N]`je totéž jako `pole[pole.length - N]`. | ||
Vytvořteproxy, která bude toto chování implementovat. | ||
Měla by fungovat takto: | ||
```js | ||
letpole = [1, 2, 3]; | ||
pole = new Proxy(pole, { | ||
/*váš kód */ | ||
}); | ||
alert(pole[-1] ); // 3 | ||
alert(pole[-2] ); // 2 | ||
//Ostatní funkcionalita pole by měla zůstat nezměněná | ||
``` |
46 changes: 23 additions & 23 deletions1-js/99-js-misc/01-proxy/03-observable/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,40 +1,40 @@ | ||
Řešení se skládá ze dvou částí: | ||
1.Kdykoli je zavoláno `.pozoruj(handler)`, musíme sihandlerněkde pamatovat, abychom jej mohli volat později. Handlery si můžeme ukládat rovnou do objektu a jako klíč vlastnosti použít náš symbol. | ||
2.Potřebujemeproxys pastí`set`, která v případě jakékoli změny zavolá handlery. | ||
```js run | ||
lethandlery = Symbol('handlery'); | ||
functionučiňPozorovatelným(cíl) { | ||
// 1.Inicializace skladu handlerů | ||
cíl[handlery] = []; | ||
//Uložíme funkci handleru do pole pro budoucí volání | ||
cíl.pozoruj = function(handler) { | ||
this[handlery].push(handler); | ||
}; | ||
// 2.Vytvořímeproxypro zpracování změn | ||
return new Proxy(cíl, { | ||
set(cíl, vlastnost, hodnota, příjemce) { | ||
letúspěch = Reflect.set(...arguments); //předáme operaci objektu | ||
if (úspěch) { //pokud při nastavování vlastnosti nedošlo k chybě, | ||
//zavoláme všechny handlery | ||
cíl[handlery].forEach(handler => handler(vlastnost, hodnota)); | ||
} | ||
returnúspěch; | ||
} | ||
}); | ||
} | ||
letuživatel = {}; | ||
uživatel =učiňPozorovatelným(uživatel); | ||
uživatel.pozoruj((klíč, hodnota) => { | ||
alert(`SET ${klíč}=${hodnota}`); | ||
}); | ||
uživatel.jméno = "Jan"; | ||
``` |
26 changes: 13 additions & 13 deletions1-js/99-js-misc/01-proxy/03-observable/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
#Pozorovatelný objekt | ||
Vytvořte funkci `učiňPozorovatelným(cíl)`, která „učiní objekt pozorovatelným“ tím, že vrátí proxy. | ||
Mělo by to fungovat takto: | ||
```js run | ||
functionučiňPozorovatelným(cíl) { | ||
/*váš kód */ | ||
} | ||
letuživatel = {}; | ||
uživatel =učiňPozorovatelným(uživatel); | ||
uživatel.pozoruj((klíč, hodnota) => { | ||
alert(`SET ${klíč}=${hodnota}`); | ||
}); | ||
uživatel.jméno = "Jan"; //oznámí: SETjméno=Jan | ||
``` | ||
Jinými slovy, objekt vrácený funkcí `učiňPozorovatelným` je stejný jako původní, ale navíc obsahuje metodu `pozoruj(handler)`, která nastaví funkci`handler`tak, aby byla volána při každé změně vlastnosti. | ||
Kdykoli se změní některá vlastnost, je zavolán`handler(klíč, hodnota)`s jejím názvem a hodnotou. | ||
P.S.V této úloze se postarejte jen o zápis do vlastnosti. Ostatní operace mohou být implementovány podobně. |
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.