Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Arrow functions revisited#173
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
122 changes: 61 additions & 61 deletions1-js/06-advanced-functions/12-arrow-functions/article.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,126 +1,126 @@ | ||
#Šipkové funkce podruhé | ||
Podívejme se nyní znovu na šipkové funkce. | ||
Šipkové funkce nejsou jen „zkratka“ pro zápis krátkého kódu. Mají určité velmi specifické a užitečné vlastnosti. | ||
V JavaScriptu nastává spousta situací, kdy potřebujeme napsat malou funkci, která se bude spouštět někde jinde. | ||
Například: | ||
- `pole.forEach(funkce)` -- `funkce` je spouštěna funkcí `forEach`pro každý prvek pole. | ||
- `setTimeout(funkce)` -- `funkce` je spouštěna zabudovaným plánovačem. | ||
- ...a mnoho dalších. | ||
Vytvořit funkci a někam ji předat je zcela v duchu JavaScriptu. | ||
A v takových funkcích obvykle nechceme opustit aktuální kontext. Tehdy šipkové funkce přijdou vhod. | ||
##Šipkové funkce nemají „this“ | ||
Jak si pamatujeme z kapitoly<info:object-methods>,šipkové funkce nemají`this`.Jestliže k`this`přistupujeme, vezme se zvnějšku. | ||
Například je můžeme použít k iteraci uvnitř objektové metody: | ||
```js run | ||
letskupina = { | ||
název: "Naše skupina", | ||
studenti: ["Jan", "Petr", "Alice"], | ||
zobrazSeznam() { | ||
*!* | ||
this.studenti.forEach( | ||
student => alert(this.název + ': ' + student) | ||
); | ||
*/!* | ||
} | ||
}; | ||
skupina.zobrazSeznam(); | ||
``` | ||
Zde je ve`forEach` použita šipková funkce, takže `this.název` v ní je přesně stejný jako ve vnější metodě `zobrazSeznam`.Tedy: `skupina.název`. | ||
Kdybychom použili „obyčejnou“ funkci, nastala by chyba: | ||
```js run | ||
letskupina = { | ||
název: "Naše skupina", | ||
studenti: ["Jan", "Petr", "Alice"], | ||
zobrazSeznam() { | ||
*!* | ||
this.studenti.forEach(function(student) { | ||
//Chyba: Nelze načíst vlastnost 'název' z undefined | ||
alert(this.název + ': ' + student) | ||
}); | ||
*/!* | ||
} | ||
}; | ||
skupina.zobrazSeznam(); | ||
``` | ||
Chyba nastane proto, že `forEach`standardně spouští funkci s`this=undefined`, takže je učiněn pokus o přístup k `undefined.název`. | ||
Na šipkové funkce to vliv nemá, protože ty prostě`this` nemají. | ||
```warn header="Šipkové funkce nelze spouštět s `new`" | ||
Neexistence`this`pochopitelně znamená další omezení: šipkové funkce nelze používat jako konstruktory. Nemůžeme je volat s `new`. | ||
``` | ||
```smart header="Šipkové funkce versus bind" | ||
Existuje drobný rozdíl mezi šipkovou funkcí`=>`a obyčejnou funkcí volanou pomocí `.bind(this)`: | ||
- `.bind(this)`vytvoří „vázanou verzi“ funkce. | ||
-Šipka`=>`žádnou vazbu nevytváří. Funkce prostě nemá`this`.Hledání`this`se provádí přesně stejným způsobem jako hledání běžné proměnné: ve vnějším lexikálním prostředí. | ||
``` | ||
##Šipkové funkce nemají „arguments“ | ||
Šipkové funkce také nemají proměnnou `arguments`. | ||
To je vynikající pro dekorátory, když potřebujeme přesměrovat volání s aktuálním`this`a `arguments`. | ||
Například `odlož(f, ms)`přijímá funkci a vrací obal kolem ní, který odloží její volání o`ms`milisekund: | ||
```js run | ||
functionodlož(f, ms) { | ||
return function() { | ||
setTimeout(() => f.apply(this, arguments), ms) | ||
}; | ||
} | ||
functionřekniAhoj(kdo) { | ||
alert('Ahoj, ' +kdo); | ||
} | ||
letřekniAhojOdložené =odlož(řekniAhoj, 2000); | ||
řekniAhojOdložené("Jan"); //Ahoj, Jan po 2sekundách | ||
``` | ||
Totéž bez šipkové funkce by vypadalo následovně: | ||
```js | ||
functionodlož(f, ms) { | ||
return function(...argumenty) { | ||
letkontext = this; | ||
setTimeout(function() { | ||
return f.apply(kontext, argumenty); | ||
}, ms); | ||
}; | ||
} | ||
``` | ||
Zde jsme museli vytvořit navíc proměnné `argumenty` a `kontext`, aby je mohla převzít funkce uvnitř`setTimeout`. | ||
##Shrnutí | ||
Šipkové funkce: | ||
-Nemají`this`. | ||
-Nemají`arguments`. | ||
-Nemohou být volány s `new`. | ||
-Nemají také`super`,ale to jsme ještě neprostudovali. Učiníme tak v kapitole<info:class-inheritance>. | ||
Je to proto, že jsou určeny pro krátké části kódu, které nemají svůj vlastní „kontext“, ale místo toho pracují v aktuálním. A v tomto způsobu použití opravdu vynikají. |
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.