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
Kai funkcija iškviečiama eilutėse `(*)` ir `(**)`, perduotos vertės perkeliamos į lokalinius kintamuosius from ir text. Tada jie naudojami funkcijos turinyje.
156
156
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ą:
158
158
159
159
160
160
```jsrun
161
161
functionshowMessage(from, text) {
162
162
163
163
*!*
164
-
from ='*'+ from +'*'; //make "from" look nicer
164
+
from ='*'+ from +'*'; //papuoškime from
165
165
*/!*
166
166
167
167
alert( from +': '+ text );
168
168
}
169
169
170
-
letfrom="Ann";
170
+
letfrom="Ana";
171
171
172
-
showMessage(from, "Hello"); // *Ann*: Hello
172
+
showMessage(from, "Hello"); // *Ana*: Sveiki
173
173
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
176
176
```
177
177
178
-
## Default values
178
+
## Numatytuosius parametrus
179
179
180
-
If a parameter is not provided, then its value becomes`undefined`.
180
+
Jei parametras nenurodytas, jo reikšmė tampa`undefined`.
181
181
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:
183
183
184
184
```js
185
-
showMessage("Ann");
185
+
showMessage("Ana");
186
186
```
187
187
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`.
189
189
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`=`:
191
191
192
192
```jsrun
193
-
functionshowMessage(from, *!*text="no text given"*/!*) {
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"`.
201
201
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:
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.
Ankstyvosios "JavaScript" versijos nepalaiko numatytųjų parametrų. Todėl senesniuose skriptiniuose tekstuose galima rasti alternatyvių būdų.
219
219
220
-
For instance, an explicit check for being `undefined`:
220
+
Pavyzdžiui, patikrinimas dėl`undefined`:
221
221
222
222
```js
223
223
functionshowMessage(from, text) {
224
224
*!*
225
225
if (text ===undefined) {
226
-
text ='no text given';
226
+
text ='tekstas nepridėtas';
227
227
}
228
228
*/!*
229
229
230
230
alert( from +": "+ text );
231
231
}
232
232
```
233
233
234
-
...Or the `||` operator:
234
+
...Arba naudodami operatorių `||`:
235
235
236
236
```js
237
237
functionshowMessage(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';
240
240
...
241
241
}
242
242
```
@@ -245,11 +245,11 @@ function showMessage(from, text) {
245
245
````
246
246
247
247
248
-
## Returning a value
248
+
## Grąžintina vertė
249
249
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.
251
251
252
-
The simplest example would be a function that sums two values:
252
+
Paprasčiausias pavyzdys - dviejų skaičių sudėjimo funkcija:
253
253
254
254
```jsrunno-beautify
255
255
functionsum(a, b) {
@@ -260,9 +260,9 @@ let result = sum(1, 2);
260
260
alert( result ); // 3
261
261
```
262
262
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`).
264
264
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:
266
266
267
267
```js run
268
268
function checkAge(age) {
@@ -272,23 +272,23 @@ function checkAge(age) {
272
272
*/!*
273
273
} else {
274
274
*!*
275
-
returnconfirm('Do you have permission from your parents?');
275
+
return confirm('Otėvaileido?');
276
276
*/!*
277
277
}
278
278
}
279
279
280
-
letage=prompt('How old are you?', 18);
280
+
let age = prompt('Kiek jums metų?', 18);
281
281
282
282
if ( checkAge(age) ) {
283
-
alert( 'Access granted' );
283
+
alert( 'Suteikta prieiga' );
284
284
} else {
285
-
alert( 'Access denied' );
285
+
alert( 'Prieiga uždrausta' );
286
286
}
287
287
```
288
288
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.
290
290
291
-
For example:
291
+
Pavyzdžiui:
292
292
293
293
```js
294
294
function showMovie(age) {
@@ -298,23 +298,23 @@ function showMovie(age) {
298
298
*/!*
299
299
}
300
300
301
-
alert( "Showing you the movie" ); // (*)
301
+
alert( "Jums rodomas filmas" ); // (*)
302
302
// ...
303
303
}
304
304
```
305
305
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`.
307
307
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ę:
310
310
311
311
```js run
312
-
functiondoNothing() { /*empty*/ }
312
+
function doNothing() { /* tuščia */ }
313
313
314
314
alert( doNothing() === undefined ); // true
315
315
```
316
316
317
-
An empty `return`is also the same as`returnundefined`:
317
+
Tuščias `return` yra analogiškas `return undefined`:
````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:
330
330
331
331
```js
332
332
return
333
333
(some + long + expression + or + whatever * f(a) + f(b))
334
334
```
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:
336
336
337
337
```js
338
338
return*!*;*/!*
339
339
(some + long + expression + or + whatever * f(a) + f(b))
340
340
```
341
341
342
-
So, it effectively becomes an empty return.
342
+
Tai iš tikrųjų tampa tuščiu `return`.
343
343
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:
345
345
346
346
```js
347
347
return (
@@ -350,46 +350,46 @@ return (
350
350
whatever * f(a) + f(b)
351
351
)
352
352
```
353
-
And it will work just as we expect it to.
353
+
Tada viskas veiks taip, kaip buvo numatyta.
354
354
````
355
355
356
-
## Naming a function [#function-naming]
356
+
## Funkcijos pavadinimo pasirinkimas
357
357
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.
359
359
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ų.
361
361
362
-
For instance, functions that start with `"show"` usually show something.
362
+
Pavyzdžiui, funkcijos, prasidedančios žodžiu "show", paprastai ką nors parodo.
363
363
364
-
Function starting with...
364
+
Funkcijos, prasidedančios nuo...
365
365
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.
370
370
371
-
Examples of such names:
371
+
Tokių pavadinimų pavyzdžiai:
372
372
373
373
```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
379
379
```
380
380
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.
382
382
383
-
```smartheader="One function -- one action"
384
-
Afunction 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.
385
385
386
-
Two independent actions usually deserve two functions, even if they are usually called together (inthatcasewecanmakea3rdfunction 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).
387
387
388
-
A few examples of breaking this rule:
388
+
Štai keletas pavyzdžių, kurie pažeidžia šią taisyklę:
389
389
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ą).
393
393
394
394
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.
0 commit comments