|
1 |
| -# Arrow functions, the basics |
| 1 | +# Šipkové funkce (arrow funkce) – základy |
| 2 | +Existuje ještě jedna velice jednoduchá a výstižná syntaxe vytváření funkcí, která často bývá lepší než funkční výrazy. |
2 | 3 |
|
3 |
| -There's another very simple and concise syntax for creating functions, that's often better than Function Expressions. |
4 |
| - |
5 |
| -It's called "arrow functions", because it looks like this: |
| 4 | +Nazývá se „šipková funkce“, jelikož vypadá takto: |
6 | 5 |
|
7 | 6 | ```js
|
8 |
| -let func = (arg1, arg2, ..., argN) => expression; |
| 7 | +let funkce = (arg1, arg2, ..., argN) => výraz; |
9 | 8 | ```
|
10 | 9 |
|
11 |
| -This creates a function `func` that accepts arguments `arg1..argN`, then evaluates the `expression` on the right side with their use and returns its result. |
| 10 | +Tím se vytvoří funkce `funkce`, která přijímá argumenty `arg1..argN`, pak s jejich použitím vyhodnotí `výraz` na pravé straně a vrátí jeho výsledek. |
12 | 11 |
|
13 |
| -In other words, it's the shorter version of: |
| 12 | +Jinými slovy, je to kratší verze tohoto: |
14 | 13 |
|
15 | 14 | ```js
|
16 |
| -let func = function(arg1, arg2, ..., argN) { |
17 |
| - return expression; |
| 15 | +let funkce = function(arg1, arg2, ..., argN) { |
| 16 | + return výraz; |
18 | 17 | };
|
19 | 18 | ```
|
20 | 19 |
|
21 |
| -Let's see a concrete example: |
| 20 | +Podívejme se na konkrétní příklad: |
22 | 21 |
|
23 | 22 | ```js run
|
24 |
| -let sum = (a, b) => a + b; |
| 23 | +let součet = (a, b) => a + b; |
25 | 24 |
|
26 |
| -/* This arrow function is a shorter form of: |
| 25 | +/* Tato šipková funkce je zkrácenou formou této funkce: |
27 | 26 |
|
28 |
| -let sum = function(a, b) { |
| 27 | +let součet = function(a, b) { |
29 | 28 | return a + b;
|
30 | 29 | };
|
31 | 30 | */
|
32 | 31 |
|
33 |
| -alert( sum(1, 2) ); // 3 |
| 32 | +alert( součet(1, 2) ); // 3 |
34 | 33 | ```
|
35 | 34 |
|
36 |
| -As you can see, `(a, b) => a + b` means a function that accepts two arguments named `a` and `b`. Upon the execution, it evaluates the expression `a + b` and returns the result. |
| 35 | +Jak vidíte, `(a, b) => a + b` značí funkci, která má dva parametry `a` a `b`. Když je vykonána, vyhodnotí výraz `a + b` a vrátí jeho výsledek. |
37 | 36 |
|
38 |
| -- If we have only one argument, then parentheses around parameters can be omitted, making that even shorter. |
| 37 | +- Máme-li pouze jeden parametr, můžeme závorky kolem něj vynechat, čímž se zápis ještě zkrátí. |
39 | 38 |
|
40 |
| - For example: |
| 39 | + Příklad: |
41 | 40 |
|
42 | 41 | ```js run
|
43 | 42 | *!*
|
44 |
| - let double = n => n * 2; |
45 |
| - // roughly the same as: let double = function(n) { return n * 2 } |
| 43 | + let dvojnásobek = n => n * 2; |
| 44 | + // zhruba totéž jako: let dvojnásobek = function(n) { return n * 2 } |
46 | 45 | */!*
|
47 | 46 |
|
48 |
| - alert( double(3) ); // 6 |
| 47 | + alert( dvojnásobek(3) ); // 6 |
49 | 48 | ```
|
50 | 49 |
|
51 |
| -- If there are no arguments, parentheses are empty, but they must be present: |
| 50 | +- Nejsou-li žádné parametry, závorky budou prázdné, ale musí být uvedeny: |
52 | 51 |
|
53 | 52 | ```js run
|
54 |
| - let sayHi = () => alert("Hello!"); |
| 53 | + let řekniAhoj = () => alert("Ahoj!"); |
55 | 54 |
|
56 |
| - sayHi(); |
| 55 | + řekniAhoj(); |
57 | 56 | ```
|
58 | 57 |
|
59 |
| -Arrow functions can be used in the same way as Function Expressions. |
| 58 | +Šipkové funkce můžeme používat stejným způsobem jako funkční výrazy. |
60 | 59 |
|
61 |
| -For instance, to dynamically create a function: |
| 60 | +Například k dynamickému vytvoření funkce: |
62 | 61 |
|
63 | 62 | ```js run
|
64 |
| -let age = prompt("What is your age?", 18); |
| 63 | +let věk = prompt("Kolik je vám let?", 18); |
65 | 64 |
|
66 |
| -let welcome = (age < 18) ? |
67 |
| - () => alert('Hello!') : |
68 |
| - () => alert("Greetings!"); |
| 65 | +let uvítání = (věk < 18) ? |
| 66 | + () => alert('Ahoj!') : |
| 67 | + () => alert("Dobrý den!"); |
69 | 68 |
|
70 |
| -welcome(); |
| 69 | +uvítání(); |
71 | 70 | ```
|
72 | 71 |
|
73 |
| -Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure. |
| 72 | +Šipkové funkce mohou na první pohled vypadat podivně a nepříliš čitelně, ale to se rychle změní, jakmile si oči na tuto strukturu zvyknou. |
74 | 73 |
|
75 |
| -They are very convenient for simple one-line actions, when we're just too lazy to write many words. |
| 74 | +Jsou velmi vhodné pro jednoduché jednořádkové akce, kdy se nám prostě nechce psát příliš mnoho slov. |
76 | 75 |
|
77 |
| -## Multiline arrow functions |
| 76 | +## Víceřádkové šipkové funkce |
78 | 77 |
|
79 |
| -The arrow functions that we've seen so far were very simple. They took arguments from the left of `=>`, evaluated and returned the right-side expression with them. |
| 78 | +Šipkové funkce, které jsme doposud viděli, byly velmi jednoduché. Přebíraly argumenty z levé strany `=>`, vyhodnotily s nimi výraz na pravé straně a vrátily jeho hodnotu. |
80 | 79 |
|
81 |
| -Sometimes we need a more complex function, with multiple expressions and statements. In that case, we can enclose them in curly braces. The major difference is that curly braces require a `return` within them to return a value (just like a regular function does). |
| 80 | +Někdy potřebujeme složitější funkci s více výrazy a příkazy. V takovém případě je můžeme uzavřít do složených závorek. Hlavní rozdíl je v tom, že složené závorky vyžadují uvnitř `return`, aby mohly vrátit hodnotu (stejně jako běžná funkce). |
82 | 81 |
|
83 |
| -Like this: |
| 82 | +Například takto: |
84 | 83 |
|
85 | 84 | ```js run
|
86 |
| -let sum = (a, b) => { // the curly brace opens a multiline function |
87 |
| - let result = a + b; |
| 85 | +let součet = (a, b) => { // složená závorka uvozuje víceřádkovou funkci |
| 86 | + let výsledek = a + b; |
88 | 87 | *!*
|
89 |
| - return result; // if we use curly braces, then we need an explicit "return" |
| 88 | + return výsledek; // když používáme složené závorky, musíme výslovně uvést „return“ |
90 | 89 | */!*
|
91 | 90 | };
|
92 | 91 |
|
93 |
| -alert( sum(1, 2) ); // 3 |
| 92 | +alert( součet(1, 2) ); // 3 |
94 | 93 | ```
|
95 | 94 |
|
96 |
| -```smart header="More to come" |
97 |
| -Here we praised arrow functions for brevity. But that's not all! |
| 95 | +```smart header="Bude toho víc" |
| 96 | +Zde jsme chválili šipkové funkce pro jejich stručnost, ale to ještě není všechno! |
98 | 97 |
|
99 |
| -Arrow functions have other interesting features. |
| 98 | +Šipkové funkce mají i jiné zajímavé vlastnosti. |
100 | 99 |
|
101 |
| -To study them in-depth, we first need to get to know some other aspects of JavaScript, so we'll return to arrow functions later in the chapter <info:arrow-functions>. |
| 100 | +Abychom je mohli prostudovat do hloubky, musíme napřed poznat některé další prvky JavaScriptu. K šipkovým funkcím se tedy vrátíme později v kapitole <info:arrow-functions>. |
102 | 101 |
|
103 |
| -For now, we can already use arrow functions for one-line actions and callbacks. |
| 102 | +Prozatím už můžeme používat šipkové funkce pro jednořádkové akce a callbacky. |
104 | 103 | ```
|
105 | 104 |
|
106 |
| -## Summary |
| 105 | +## Shrnutí |
107 | 106 |
|
108 |
| -Arrow functions are handy for simple actions, especially for one-liners. They come in two flavors: |
| 107 | +Šipkové funkce se hodí pro jednoduché akce, zvláště pro jednořádkové funkce. Dají se napsat dvěma způsoby: |
109 | 108 |
|
110 |
| -1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result. Parentheses can be omitted, if there's only a single argument, e.g. `n => n*2`. |
111 |
| -2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something. |
| 109 | +1. Bez složených závorek: `(...args) => výraz` -- na pravé straně je výraz: funkce jej vyhodnotí a vrátí jeho výsledek. Kulaté závorky můžeme vynechat, má-li funkce pouze jeden parametr, např. `n => n*2`. |
| 110 | +2. Se složenými závorkami: `(...args) => { tělo }` -- složené závorky nám umožňují uvést ve funkci více příkazů, ale aby funkce něco vrátila, musíme výslovně uvést `return`. |
0 commit comments