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
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/14-function-basics/article.md
+30-30Lines changed: 30 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,40 +1,40 @@
1
1
# Functions
2
2
3
-
Quite often we need to perform a similar action in many places of the script.
3
+
Dažnai tą patį veiksmą reikia pakartoti daugelyje programos dalių.
4
4
5
-
For example, we need to show a nice-looking message when a visitor logs in, logs out and maybe somewhere else.
5
+
Pavyzdžiui, reikia parodyti lankytojui gražų pranešimą, kai jis įeina į svetainę, kai jis išeina iš svetainės arba bet kur kitur.
6
6
7
-
Functions are the main "building blocks" of the program. They allow the code to be called many times without repetition.
7
+
Siekiant išvengti to paties kodo kartojimo daugelyje vietų, buvo sugalvotos funkcijos. Funkcijos - tai pagrindiniai programos blokai.
8
8
9
-
We've already seen examples of built-in functions, like `alert(message)`, `prompt(message, default)`and`confirm(question)`. But we can create functions of our own as well.
9
+
Jus jau matėte integruotų funkcijų pavyzdžių - tai `alert(message)`, `prompt(message, default)`ir`confirm(question)`. Tačiau taip pat galima kurti savo funkcijas
10
10
11
-
## Function Declaration
11
+
## Funkcijos deklaravimas
12
12
13
-
To create a function we can use a *function declaration*.
13
+
Norėdami sukurti funkcijas, galime naudoti *funkcijos deklaravimų*.
14
14
15
-
It looks like this:
15
+
Funkcijos deklaravimo pavyzdys:
16
16
17
17
```js
18
18
functionshowMessage() {
19
-
alert( 'Hello everyone!' );
19
+
alert( 'Labas visiems!' );
20
20
}
21
21
```
22
22
23
-
The `function` keyword goes first, then goes the *name of the function*, then a list of *parameters* between the parentheses (comma-separated, empty in the example above) and finally the code of the function, also named "the function body", between curly braces.
23
+
Pirmiausia įrašomas raktažodis `funkcija', po jo nurodomas funkcijos pavadinimas, po to skliausteliuose pateikiamas parametrų sąrašas, atskirtas kableliais (aukščiau pateiktame pavyzdyje jis yra tuščias), ir, galiausiai, funkcijos kodas, dar vadinamas «funkcijos turiniu», riestiniuose skliaustuose.
24
24
25
25
```js
26
26
functionname(parameters) {
27
27
...body...
28
28
}
29
29
```
30
30
31
-
Our new function can be called by its name: `showMessage()`.
31
+
Mūsų naująją funkciją galima iškviesti jos pavadinimu: `showMessage()`.
32
32
33
-
For instance:
33
+
Pavyzdžiui:
34
34
35
35
```js run
36
36
functionshowMessage() {
37
-
alert( 'Hello everyone!' );
37
+
alert( 'Labas visiems!' );
38
38
}
39
39
40
40
*!*
@@ -43,66 +43,66 @@ showMessage();
43
43
*/!*
44
44
```
45
45
46
-
The call `showMessage()` executes the code of the function. Here we will see the message two times.
46
+
Kai mes iškviečiame `showMessage()`, įvykdomas funkcijos kodas. Čia matysime pranešimą du kartus.
47
47
48
-
This example clearly demonstrates one of the main purposes of functions: to avoid code duplication.
48
+
Šis pavyzdys aiškiai parodo vieną iš pagrindinių funkcijų tikslų - atsikratyti kodo dubliavimo.
49
49
50
-
If we ever need to change the message or the way it is shown, it's enough to modify the code in one place: the function which outputs it.
50
+
Jei reikia pakeisti pranešimą arba jo išvedimo būdą, užteks pakeisti ją vienoje vietoje: funkcijoje, kuri išveda šį pranešimą.
51
51
52
-
## Local variables
52
+
## Lokaliniai kintamieji
53
53
54
-
A variable declared inside a function is only visible inside that function.
54
+
Funkcijoje deklaruoti kintamieji matomi tik toje funkcijoje.
55
55
56
-
For example:
56
+
Pavyzdžiui:
57
57
58
58
```js run
59
59
functionshowMessage() {
60
60
*!*
61
-
let message ="Hello, I'm JavaScript!"; //local variable
61
+
let message ="Sveiki, aš esu JavaScript!"; //lokalinis kintamasis
62
62
*/!*
63
63
64
64
alert( message );
65
65
}
66
66
67
-
showMessage(); //Hello, I'm JavaScript!
67
+
showMessage(); //Sveiki, aš esu JavaScript!
68
68
69
69
alert( message ); // <-- Error! The variable is local to the function
70
70
```
71
71
72
-
## Outer variables
72
+
## Išoriniai kintamieji
73
73
74
-
A function can access an outer variable as well, for example:
74
+
Funkcija turi prieigą prie išorinių kintamųjų, pavyzdžiui:
75
75
76
76
```js run no-beautify
77
77
let*!*userName*/!*='John';
78
78
79
79
functionshowMessage() {
80
-
let message ='Hello, '+*!*userName*/!*;
80
+
let message ='Labas, '+*!*userName*/!*;
81
81
alert(message);
82
82
}
83
83
84
-
showMessage(); //Hello, John
84
+
showMessage(); //Labas, John
85
85
```
86
86
87
-
The function has full access to the outer variable. It can modify it as well.
87
+
Funkcija turi pilną prieigą prie išorinių kintamųjų ir gali keisti jų vertę.
88
88
89
-
For instance:
89
+
Pavyzdžiui:
90
90
91
91
```js run
92
92
let*!*userName*/!*='John';
93
93
94
94
functionshowMessage() {
95
-
*!*userName*/!* = "Bob"; // (1) changed the outer variable
0 commit comments