You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's another very simple and concise syntax for creating functions, that's often better than Function Expressions.
3
+
Yra dar viena labai paprasta ir glausta funkcijų kūrimo sintaksė, kuri dažnai yra geresnė už Function Expressions.
4
4
5
-
It's called "arrow functions", because it looks like this:
5
+
Ji vadinama “rodyklių funkcijomis” (ang. *“arrow functions”*), nes atrodo taip:
6
6
7
7
```js
8
8
letfunc= (arg1, arg2, ..., argN) => expression;
9
9
```
10
10
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.
11
+
Čia sukuriama funkcija `func`, kuri priima argumentus`arg1..argN`, tada įvertina dešinėje pusėje esantį `expression`su jų panaudojimu ir grąžina rezultatą.
12
12
13
-
In other words, it's the shorter version of:
13
+
Kitaip tariant, tai yra trumpesnė versija:
14
14
15
15
```js
16
16
letfunc=function(arg1, arg2, ..., argN) {
17
17
return expression;
18
18
};
19
19
```
20
20
21
-
Let's see a concrete example:
21
+
Išnagrinėkime konkretų pavyzdį:
22
22
23
23
```js run
24
24
letsum= (a, b) => a + b;
25
25
26
-
/*This arrow function is a shorter form of:
26
+
/*Ši rodyklės funkcija yra trumpesnė forma:
27
27
28
28
let sum = function(a, b) {
29
29
return a + b;
@@ -33,79 +33,79 @@ let sum = function(a, b) {
33
33
alert( sum(1, 2) ); // 3
34
34
```
35
35
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.
36
+
Kaip jūs matote, `(a, b) => a + b`reiškia funkciją, kuri priima du argumentus, pavadintus `a`ir`b`. Vykdymo metu ji įvertina išraišką `a + b`ir grąžina rezultatą.
37
37
38
-
-If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.
38
+
-Jeigu mes turime tik vieną argumentą, skliaustelius aplink parametrus galima praleisti ir taip dar labiau sutrumpinti užrašą
39
39
40
-
For example:
40
+
Pavyzdžiui:
41
41
42
42
```js run
43
43
*!*
44
44
letdouble=n=> n *2;
45
-
//roughly the same as: let double = function(n) { return n * 2 }
45
+
//maždaug tas pats kaip: let double = function(n) { return n * 2 }
46
46
*/!*
47
47
48
48
alert( double(3) ); // 6
49
49
```
50
50
51
-
-If there are no arguments, parentheses will be empty (but they should be present):
51
+
-Jeigu argumentų nėra, skliaustai bus tušti (tačiau jie turėtų būti):
52
52
53
53
```js run
54
-
let sayHi = () => alert("Hello!");
54
+
let sayHi = () => alert("Labas!");
55
55
56
56
sayHi();
57
57
```
58
58
59
-
Arrow functions can be used in the same way asFunction Expressions.
59
+
Rodyklės funkcijas galima naudoti taip pat, kaip irFunction Expressions.
60
60
61
-
For instance, to dynamically create a function:
61
+
Pavyzdžiui, norėdami dinamiškai sukurti funkciją:
62
62
63
63
```js run
64
-
let age = prompt("What is your age?", 18);
64
+
let age = prompt("Koks jūsų amžius?", 18);
65
65
66
66
let welcome = (age < 18) ?
67
-
() => alert('Hello') :
68
-
() => alert("Greetings!");
67
+
() => alert('Labas!') :
68
+
() => alert("Laba diena!");
69
69
70
70
welcome();
71
71
```
72
72
73
-
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
73
+
Iš pradžių rodyklių funkcijos gali atrodyti nepažįstamos ir nelabai skaitomos, tačiau tai greitai pasikeičia, kai akys pripranta prie struktūros.
74
74
75
-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
75
+
Jie labai patogūs atliekant paprastus vienos eilutės veiksmus, kai tiesiog tingime rašyti daug žodžių.
76
76
77
-
## Multiline arrow functions
77
+
## Kelių eilučių rodyklių funkcijos
78
78
79
-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
79
+
Aukščiau pateiktuose pavyzdžiuose buvo paimti argumentai iš `=>`kairės pusės ir su jais įvertinta dešiniosios pusės išraiška.
80
80
81
-
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
81
+
Kartais mums reikia šiek tiek sudėtingesnių dalykų, pavyzdžiui, kelių išraiškų ar teiginių. Tai taip pat įmanoma, tačiau jas turėtume uždaryti figūriniais skliaustais. Tada juose reikia naudoti įprastą `return`.
82
82
83
-
Like this:
83
+
Štai taip:
84
84
85
85
```js run
86
-
let sum = (a, b) => { // the curly brace opens a multiline function
86
+
let sum = (a, b) => { // figūrinis skliaustelis atveria kelių eilučių funkciją
87
87
let result = a + b;
88
88
*!*
89
-
return result; // if we use curly braces, then we need an explicit "return"
89
+
return result; // jei naudojame figūrinius skliaustus, reikia aiškiai nurodyti "return".
90
90
*/!*
91
91
};
92
92
93
93
alert( sum(1, 2) ); // 3
94
94
```
95
95
96
-
```smart header="More to come"
97
-
Here we praised arrow functions for brevity. But that's not all!
96
+
```smart header="Toliau mes išmoksime daugiau"
97
+
Čia mes pateikėme pagrindinę rodyklių funkcijų paskirtį -- trumpumas. Bet tai dar ne viskas!
98
98
99
-
Arrow functions have other interesting features.
99
+
Rodyklių funkcijos turi ir kitų įdomių savybių.
100
100
101
-
To study them in-depth, we first need to get to know some other aspects ofJavaScript, so we'll return to arrow functions later in the chapter <info:arrow-functions>.
101
+
Norėdami jas nuodugniai išnagrinėti, pirmiausia turime susipažinti su kitais JavaScript aspektais, todėl prie rodyklių funkcijų grįšime vėliau, skyriuje <info:arrow-functions>.
102
102
103
-
For now, we can already use arrow functions for one-line actions and callbacks.
103
+
O dabar mes jau galime naudoti rodyklių funkcijas vienos eilutės veiksmams ir callback'ams.
104
104
```
105
105
106
-
## Summary
106
+
## Santrauka
107
107
108
-
Arrow functions are handy for one-liners. They come in two flavors:
108
+
Rodyklių funkcijos yra patogios, kai reikia parašyti vieną eilutę. Jos būna dviejų rūšių:
109
109
110
-
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
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.
110
+
1.Be figūrinių skliaustų:`(...args) => expression`--dešinioji išraiškos pusė: funkcija ją apskaičiuoja ir grąžina rezultatą. Skliaustelius galima praleisti, jei yra tik vienas argumentas:`n => n * 2`.
111
+
2.Su figūriniais skliaustais:`(...args) => { body }`--figūriniai skliaustai leidžia įrašyti kelis teiginius funkcijos viduje, tačiau mums reikia aiškaus `return`, kad ką nors grąžintume.
0 commit comments