Skip to content

Commit 1fcd180

Browse files
authored
Merge pull request #83 from otmon76/1.2.17
Arrow functions, the basics
2 parents 7c2174f + ce535ff commit 1fcd180

File tree

3 files changed

+65
-66
lines changed

3 files changed

+65
-66
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11

22
```js run
3-
function ask(question, yes, no) {
4-
if (confirm(question)) yes();
5-
else no();
3+
function zeptejSe(otázka, ano, ne) {
4+
if (confirm(otázka)) ano();
5+
else ne();
66
}
77

8-
ask(
9-
"Do you agree?",
8+
zeptejSe(
9+
"Souhlasíte?",
1010
*!*
11-
() => alert("You agreed."),
12-
() => alert("You canceled the execution.")
11+
() => alert("Souhlasil jste."),
12+
() => alert("Zrušil jste provádění.")
1313
*/!*
1414
);
1515
```
1616

17-
Looks short and clean, right?
17+
Vypadá to stručně a čistě, že?
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11

2-
# Rewrite with arrow functions
2+
# Přepište na šipkové funkce
33

4-
Replace Function Expressions with arrow functions in the code below:
4+
Přepište funkční výrazy v následujícím kódu na šipkové funkce:
55

66
```js run
7-
function ask(question, yes, no) {
8-
if (confirm(question)) yes();
9-
else no();
7+
function zeptejSe(otázka, ano, ne) {
8+
if (confirm(otázka)) ano();
9+
else ne();
1010
}
1111

12-
ask(
13-
"Do you agree?",
14-
function() { alert("You agreed."); },
15-
function() { alert("You canceled the execution."); }
12+
zeptejSe(
13+
"Souhlasíte?",
14+
function() { alert("Souhlasil jste."); },
15+
function() { alert("Zrušil jste provádění."); }
1616
);
1717
```
Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,110 @@
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.
23

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:
65

76
```js
8-
let func = (arg1, arg2, ..., argN) => expression;
7+
let funkce = (arg1, arg2, ..., argN) => výraz;
98
```
109

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.
1211

13-
In other words, it's the shorter version of:
12+
Jinými slovy, je to kratší verze tohoto:
1413

1514
```js
16-
let func = function(arg1, arg2, ..., argN) {
17-
return expression;
15+
let funkce = function(arg1, arg2, ..., argN) {
16+
return výraz;
1817
};
1918
```
2019

21-
Let's see a concrete example:
20+
Podívejme se na konkrétní příklad:
2221

2322
```js run
24-
let sum = (a, b) => a + b;
23+
let součet = (a, b) => a + b;
2524

26-
/* This arrow function is a shorter form of:
25+
/* Tato šipková funkce je zkrácenou formou této funkce:
2726
28-
let sum = function(a, b) {
27+
let součet = function(a, b) {
2928
return a + b;
3029
};
3130
*/
3231

33-
alert( sum(1, 2) ); // 3
32+
alert( součet(1, 2) ); // 3
3433
```
3534

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.
3736

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í.
3938

40-
For example:
39+
Příklad:
4140

4241
```js run
4342
*!*
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 }
4645
*/!*
4746

48-
alert( double(3) ); // 6
47+
alert( dvojnásobek(3) ); // 6
4948
```
5049

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:
5251

5352
```js run
54-
let sayHi = () => alert("Hello!");
53+
let řekniAhoj = () => alert("Ahoj!");
5554
56-
sayHi();
55+
řekniAhoj();
5756
```
5857

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.
6059

61-
For instance, to dynamically create a function:
60+
Například k dynamickému vytvoření funkce:
6261

6362
```js run
64-
let age = prompt("What is your age?", 18);
63+
let věk = prompt("Kolik je vám let?", 18);
6564
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!");
6968
70-
welcome();
69+
uvítání();
7170
```
7271

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.
7473

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.
7675

77-
## Multiline arrow functions
76+
## Víceřádkové šipkové funkce
7877

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.
8079

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).
8281

83-
Like this:
82+
Například takto:
8483

8584
```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;
8887
*!*
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
9089
*/!*
9190
};
9291
93-
alert( sum(1, 2) ); // 3
92+
alert( součet(1, 2) ); // 3
9493
```
9594

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!
9897
99-
Arrow functions have other interesting features.
98+
Šipkové funkce mají i jiné zajímavé vlastnosti.
10099
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>.
102101
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.
104103
```
105104

106-
## Summary
105+
## Shrnutí
107106

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:
109108

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

Comments
 (0)