Skip to content

Commit 900b8ef

Browse files
authored
80%
1 parent b6d05e3 commit 900b8ef

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

1-js/02-first-steps/14-function-basics/article.md

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -154,89 +154,89 @@ showMessage('Ana', "Kaip sekasi?"); // Ann: Kaip sekasi? (**)
154154
155155
Kai funkcija iškviečiama eilutėse `(*)` ir `(**)`, perduotos vertės perkeliamos į lokalinius kintamuosius from ir text. Tada jie naudojami funkcijos turinyje.
156156
157-
Here's one more example: we have a variable `from` and pass it to the function. Please note: the function changes `from`, but the change is not seen outside, because a function always gets a copy of the value:
157+
Štai dar vienas pavyzdys: turime kintamąjį `from` ir perduodame jį funkcijai. Atkreipkite dėmesį: funkcija pakeičia reikšmę `from`, tačiau šis pokytis nėra matomas iš išorės. Funkcija visada gauna tik vertės kopiją:
158158
159159
160160
```js run
161161
function showMessage(from, text) {
162162

163163
*!*
164-
from = '*' + from + '*'; // make "from" look nicer
164+
from = '*' + from + '*'; // papuoškime from
165165
*/!*
166166

167167
alert( from + ': ' + text );
168168
}
169169

170-
let from = "Ann";
170+
let from = "Ana";
171171

172-
showMessage(from, "Hello"); // *Ann*: Hello
172+
showMessage(from, "Hello"); // *Ana*: Sveiki
173173

174-
// the value of "from" is the same, the function modified a local copy
175-
alert( from ); // Ann
174+
// // "from" vertė lieka ta pati, funkcija pakeitė tik lokalinio kintamojo vertę
175+
alert( from ); // Ana
176176
```
177177
178-
## Default values
178+
## Numatytuosius parametrus
179179
180-
If a parameter is not provided, then its value becomes `undefined`.
180+
Jei parametras nenurodytas, jo reikšmė tampa `undefined`.
181181
182-
For instance, the aforementioned function `showMessage(from, text)` can be called with a single argument:
182+
Pavyzdžiui, aukščiau pateiktą funkciją `showMessage(from, text)` galima iškviesti su vienu argumentu:
183183
184184
```js
185-
showMessage("Ann");
185+
showMessage("Ana");
186186
```
187187
188-
That's not an error. Such a call would output `"Ann: undefined"`. There's no `text`, so it's assumed that `text === undefined`.
188+
Tai nesukels klaidos. Po tokio iškvietimo rašoma `"*Anya*: undefined"`. Kvietime nenurodytas teksto parametras, todėl laikoma, kad `text === undefined`.
189189
190-
If we want to use a "default" `text` in this case, then we can specify it after `=`:
190+
Jei norime nustatyti numatytąją `text` parametro vertę, turime ją nurodyti po `=`:
191191
192192
```js run
193-
function showMessage(from, *!*text = "no text given"*/!*) {
193+
function showMessage(from, *!*text = "tekstas nepridėtas"*/!*) {
194194
alert( from + ": " + text );
195195
}
196196

197-
showMessage("Ann"); // Ann: no text given
197+
showMessage("Ana"); // Ana: tekstas nepridėtas
198198
```
199199
200-
Now if the `text` parameter is not passed, it will get the value `"no text given"`
200+
Dabar, jei `text` parametras nenurodytas, jo vertė bus `"tekstas nepridėtas"`.
201201
202-
Here `"no text given"` is a string, but it can be a more complex expression, which is only evaluated and assigned if the parameter is missing. So, this is also possible:
202+
Šiuo atveju `"tekstas nepridėtas"` yra eilutė, tačiau vietoj jos gali būti sudėtingesnė išraiška, kuri apskaičiuojama ir priskiriama, kai nėra parametro. Pavyzdžiui:
203203
204204
```js run
205205
function showMessage(from, text = anotherFunction()) {
206-
// anotherFunction() only executed if no text given
207-
// its result becomes the value of text
206+
// anotherFunction() bus vykdoma tik tuo atveju, jei `text` nebus perduotas
207+
// rezultatas yra `text` vertė.
208208
}
209209
```
210210
211-
```smart header="Evaluation of default parameters"
212-
In JavaScript, a default parameter is evaluated every time the function is called without the respective parameter.
211+
```smart header="Numatytųjų parametrų apskaičiavimas"
212+
JavaScript kalba numatytieji parametrai apskaičiuojami kiekvieną kartą, kai funkcija iškviečiama be atitinkamo parametro.
213213

214-
In the example above, `anotherFunction()` is called every time `showMessage()` is called without the `text` parameter.
214+
Aukščiau pateiktame pavyzdyje `anotherFunction()` bus iškviečiama kiekvieną kartą, kai `showMessage()` bus iškviesta be `text` parametro.
215215
```
216216
217-
````smart header="Default parameters old-style"
218-
Old editions of JavaScript did not support default parameters. So there are alternative ways to support them, that you can find mostly in the old scripts.
217+
````smart header="Numatytųjų nustatymų naudojimas ankstesnėse JavaScript versijose"
218+
Ankstyvosios "JavaScript" versijos nepalaiko numatytųjų parametrų. Todėl senesniuose skriptiniuose tekstuose galima rasti alternatyvių būdų.
219219
220-
For instance, an explicit check for being `undefined`:
220+
Pavyzdžiui, patikrinimas dėl `undefined`:
221221
222222
```js
223223
function showMessage(from, text) {
224224
*!*
225225
if (text === undefined) {
226-
text = 'no text given';
226+
text = 'tekstas nepridėtas';
227227
}
228228
*/!*
229229

230230
alert( from + ": " + text );
231231
}
232232
```
233233
234-
...Or the `||` operator:
234+
...Arba naudodami operatorių `||`:
235235
236236
```js
237237
function showMessage(from, text) {
238-
// if text is falsy then text gets the "default" value
239-
text = text || 'no text given';
238+
// jei `text` vertę yra false, nustatyti `text` parametro numatytąją vertę
239+
text = text || 'tekstas nepridėtas';
240240
...
241241
}
242242
```
@@ -245,11 +245,11 @@ function showMessage(from, text) {
245245
````
246246
247247
248-
## Returning a value
248+
## Grąžintina vertė
249249
250-
A function can return a value back into the calling code as the result.
250+
Funkcija gali grąžinti rezultatą, kuris bus perduotas ją iškvietusiam kodui.
251251
252-
The simplest example would be a function that sums two values:
252+
Paprasčiausias pavyzdys - dviejų skaičių sudėjimo funkcija:
253253
254254
```js run no-beautify
255255
function sum(a, b) {
@@ -260,9 +260,9 @@ let result = sum(1, 2);
260260
alert( result ); // 3
261261
```
262262
263-
The directive `return` can be in any place of the function. When the execution reaches it, the function stops, and the value is returned to the calling code (assigned to `result` above).
263+
`Return` direktyva gali būti bet kurioje funkcijos turinio vietoje. Kai tik vykdymas pasiekia šį tašką, funkcija sustoja ir vertė grąžinama ją iškvietusiam kodui (priskirta aukščiau nurodytam kintamajam `result`).
264264
265-
There may be many occurrences of `return` in a single function. For instance:
265+
Funkcijoje `return' gali būti naudojamas kelis kartus, pavyzdžiui:
266266
267267
```js run
268268
function checkAge(age) {
@@ -272,23 +272,23 @@ function checkAge(age) {
272272
*/!*
273273
} else {
274274
*!*
275-
return confirm('Do you have permission from your parents?');
275+
return confirm('O tėvai leido?');
276276
*/!*
277277
}
278278
}
279279
280-
let age = prompt('How old are you?', 18);
280+
let age = prompt('Kiek jums metų?', 18);
281281
282282
if ( checkAge(age) ) {
283-
alert( 'Access granted' );
283+
alert( 'Suteikta prieiga' );
284284
} else {
285-
alert( 'Access denied' );
285+
alert( 'Prieiga uždrausta' );
286286
}
287287
```
288288
289-
It is possible to use `return` without a value. That causes the function to exit immediately.
289+
Galima naudoti `return` be vertės. Dėl to funkcija bus iš karto baigta.
290290
291-
For example:
291+
Pavyzdžiui:
292292
293293
```js
294294
function showMovie(age) {
@@ -298,23 +298,23 @@ function showMovie(age) {
298298
*/!*
299299
}
300300
301-
alert( "Showing you the movie" ); // (*)
301+
alert( "Jums rodomas filmas" ); // (*)
302302
// ...
303303
}
304304
```
305305
306-
In the code above, if `checkAge(age)` returns `false`, then `showMovie` won't proceed to the `alert`.
306+
Aukščiau pateiktame kode, jei `checkAge(age)` grąžina `false`, `showMovie` neįvykdys `alert`.
307307
308-
````smart header="A function with an empty `return` or without it returns `undefined`"
309-
If a function does not return a value, it is the same as if it returns `undefined`:
308+
````smart header="Funkcijos rezultatas su tuščiu `return` arba be jo yra `undefined`"
309+
Jei funkcija negrąžina vertės, tai yra tas pats, lyg ji būtų grąžinusi `undefined` vertę:
310310
311311
```js run
312-
function doNothing() { /* empty */ }
312+
function doNothing() { /* tuščia */ }
313313
314314
alert( doNothing() === undefined ); // true
315315
```
316316
317-
An empty `return` is also the same as `return undefined`:
317+
Tuščias `return` yra analogiškas `return undefined`:
318318
319319
```js run
320320
function doNothing() {
@@ -325,23 +325,23 @@ alert( doNothing() === undefined ); // true
325325
```
326326
````
327327
328-
````warn header="Never add a newline between `return` and the value"
329-
For a long expression in `return`, it might be tempting to put it on a separate line, like this:
328+
````warn header="Niekada nedėkite eilutės laužimo tarp `return` ir jo vertės"
329+
Ilgos išraiškos `return` atveju gali kilti pagunda ją pateikti keliose atskirose eilutėse, pvz., taip:
330330
331331
```js
332332
return
333333
(some + long + expression + or + whatever * f(a) + f(b))
334334
```
335-
That doesn't work, because JavaScript assumes a semicolon after `return`. That'll work the same as:
335+
Kodas nebus vykdomas, nes JavaScript interpretatorius po `return` įterpia kabliataškį. Jam šis kodas atrodys taip:
336336
337337
```js
338338
return*!*;*/!*
339339
(some + long + expression + or + whatever * f(a) + f(b))
340340
```
341341
342-
So, it effectively becomes an empty return.
342+
Tai iš tikrųjų tampa tuščiu `return`.
343343
344-
If we want the returned expression to wrap across multiple lines, we should start it at the same line as `return`. Or at least put the opening parentheses there as follows:
344+
Jei norime, kad grąžinimo išraiška apimtų kelias eilutes, turime ją pradėti toje pačioje eilutėje kaip ir `return`. Arba bent jau įterpti skliaustus, pvz., taip:
345345
346346
```js
347347
return (
@@ -350,46 +350,46 @@ return (
350350
whatever * f(a) + f(b)
351351
)
352352
```
353-
And it will work just as we expect it to.
353+
Tada viskas veiks taip, kaip buvo numatyta.
354354
````
355355
356-
## Naming a function [#function-naming]
356+
## Funkcijos pavadinimo pasirinkimas
357357
358-
Functions are actions. So their name is usually a verb. It should be brief, as accurate as possible and describe what the function does, so that someone reading the code gets an indication of what the function does.
358+
Funkcija - tai veiksmas. Todėl funkcijos pavadinimas dažniausiai yra veiksmažodis. Jis turi būti paprastas, tikslus ir apibūdinti funkcijos veiksmą taip, kad programuotojas, kuris skaitys kodą, teisingai suprastų, ką funkcija daro.
359359
360-
It is a widespread practice to start a function with a verbal prefix which vaguely describes the action. There must be an agreement within the team on the meaning of the prefixes.
360+
Paprastai veiksmažodžių priešdėliai vartojami bendram veiksmo pobūdžiui reikšti, o po jų eina paaiškinimas. Paprastai programuotojų komandos yra sudariusios susitarimus dėl šių priešdėlių reikšmių.
361361
362-
For instance, functions that start with `"show"` usually show something.
362+
Pavyzdžiui, funkcijos, prasidedančios žodžiu "show", paprastai ką nors parodo.
363363
364-
Function starting with...
364+
Funkcijos, prasidedančios nuo...
365365
366-
- `"get…"` -- return a value,
367-
- `"calc…"` -- calculate something,
368-
- `"create…"` -- create something,
369-
- `"check…"` -- check something and return a boolean, etc.
366+
- `"get…"` -- grąžina vertę,
367+
- `"calc…"` -- ką nors apskaičiuoja,
368+
- `"create…"` -- ką nors sukuria,
369+
- `"check…"` -- ką nors patikrina ir grąžina "Boolean" vertę ir t. t.
370370
371-
Examples of such names:
371+
Tokių pavadinimų pavyzdžiai:
372372
373373
```js no-beautify
374-
showMessage(..) // shows a message
375-
getAge(..) // returns the age (gets it somehow)
376-
calcSum(..) // calculates a sum and returns the result
377-
createForm(..) // creates a form (and usually returns it)
378-
checkPermission(..) // checks a permission, returns true/false
374+
showMessage(..) // rodo pranešimą
375+
getAge(..) // grąžina amžių
376+
calcSum(..) // apskaičiuoja sumą ir grąžina rezultatą
377+
createForm(..) // sukuria formą (ir dažniausiai ją grąžina).
378+
checkPermission(..) // tikrina prieigą ir grąžina true/false
379379
```
380380
381-
With prefixes in place, a glance at a function name gives an understanding what kind of work it does and what kind of value it returns.
381+
Dėl prefiksų iš pirmo žvilgsnio aišku, ką funkcijos pavadinimas daro ir kokią vertę gali grąžinti.
382382
383-
```smart header="One function -- one action"
384-
A function should do exactly what is suggested by its name, no more.
383+
```smart header="Viena funkcija - vienas veiksmas"
384+
Funkcija turėtų atlikti tik tai, ką aiškiai nurodo jos pavadinimas. Tai turėtų būti vienas veiksmas.
385385
386-
Two independent actions usually deserve two functions, even if they are usually called together (in that case we can make a 3rd function that calls those two).
386+
Du nepriklausomi veiksmai paprastai reiškia dvi funkcijas, net jei jos turi būti iškviestos kartu (tokiu atveju galime sukurti trečią funkciją joms iškviesti).
387387
388-
A few examples of breaking this rule:
388+
Štai keletas pavyzdžių, kurie pažeidžia šią taisyklę:
389389
390-
- `getAge` -- would be bad if it shows an `alert` with the age (should only get).
391-
- `createForm` -- would be bad if it modifies the document, adding a form to it (should only create it and return).
392-
- `checkPermission` -- would be bad if it displays the `access granted/denied` message (should only perform the check and return the result).
390+
- `getAge` -- būtų netinkamas pasirinkimas, jei funkcija išveda `alert` su amžiumi (turėtų tik grąžinti jį).
391+
- `createForm` -- būtų netinkamas pasirinkimas, jei funkcija pakeistų dokumentą, pridėdama į jį formą (turėtų tik sukurti formą ir ją grąžinti).
392+
- `checkPermission` -- būtų netinkamas pasirinkimas, jei funkcija rodytų pranešimą su tekstu "Prieiga suteikta / uždrausta" (turėtų tik atlikti patikrinimą ir grąžinti jo rezultatą).
393393
394394
These examples assume common meanings of prefixes. You and your team are free to agree on other meanings, but usually they're not much different. In any case, you should have a firm understanding of what a prefix means, what a prefixed function can and cannot do. All same-prefixed functions should obey the rules. And the team should share the knowledge.
395395
```

0 commit comments

Comments
 (0)