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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This chapter briefly recaps the features of JavaScriptthat we've learned by now, paying special attention to subtle moments.
Šiame skyriuje trumpai apžvelgiamos iki šiol išmoktos JavaScriptsavybės, ypatingą dėmesį skiriant subtiliems momentams.
##Code structure
##Kodo struktūra
Statements are delimited with a semicolon:
Teiginiai atskiriami kabliataškiu:
```js run no-beautify
alert('Hello'); alert('World');
alert('Labas'); alert('Pasauli');
```
Usually, a line-break is also treated as a delimiter, so that would also work:
Paprastai eilutės laužimas taip pat laikomas skiriamuoju ženklu, todėl jis taip pat tiktų:
```js run no-beautify
alert('Hello')
alert('World')
```
That's called "automatic semicolon insertion". Sometimes it doesn't work, for instance:
Tai vadinama “automatiniu kabliataškio įterpimu”. Kartais tai neveikia, pavyzdžiui:
```js run
alert("There will be an error after this message")
alert("Po šio pranešimo pasirodys klaida")
[1, 2].forEach(alert)
```
Most codestyle guides agree that we should put a semicolon after each statement.
Dauguma kodų stiliaus vadovų sutinka, kad po kiekvieno teiginio turėtume dėti kabliataškį.
Semicolons are not required after code blocks`{...}`and syntax constructs with them like loops:
Po kodo blokų`{...}`ir sintaksės konstrukcijų su jais, pvz., ciklų, kabliataškiai nereikalingi:
```js
function f() {
//no semicolon needed after function declaration
//po funkcijos deklaravimo nereikia kabliataškio
}
for(;;) {
//no semicolon needed after the loop
//po ciklo nereikia kabliataškio
}
```
...But even if we can put an "extra" semicolon somewhere, that's not an error. It will be ignored.
...Tačiau net jei kur nors galime įterpti papildomą kabliataškį, tai nėra klaida. Tai bus ignoruojama.
More in: <info:structure>.
Išsamiau: <info:structure>.
##Strict mode
##Griežtas režimas
To fully enable all features of modernJavaScript, we should start scripts with `"use strict"`.
Kad būtų įjungtos visos šiuolaikinėsJavaScript ypatybės, skriptus turėtume pradėti nuo `"use strict"`.
```js
'use strict';
...
```
The directive must be at the top of a script or at the beginning of a function body.
Ši direktyva turi būti skripto viršuje arba funkcijos kūno pradžioje.
Without `"use strict"`, everything still works, but some features behave in the old-fashion, "compatible" way. We'd generally prefer the modern behavior.
Be `"use strict"` viskas veikia, tačiau kai kurios funkcijos veikia senamadiškai, “suderinamumo” būdu. Apskritai mums labiau tiktų šiuolaikinis elgesys.
Some modern features of the language (like classes that we'll study in the future) enable strict mode implicitly.
Kai kurios šiuolaikinės kalbos savybės (pvz., klasės, kurias nagrinėsime ateityje) įgalina griežtąjį režimą netiesiogiai.
More in: <info:strict-mode>.
Išsamiau: <info:strict-mode>.
##Variables
##Kintamieji
Can be declared using:
Galima deklaruoti naudojant:
- `let`
- `const` (constant, can't be changed)
- `var` (old-style, will see later)
- `const` (konstanta, negali būti pakeista)
- `var` (pasenęs būdas, daugiau informacijos vėliau)
A variable name can include:
-Letters and digits, but the first character may not be a digit.
-Characters`$`and `_` are normal, on par with letters.
-Non-Latin alphabets and hieroglyphs are also allowed, but commonly not used.
Kintamojo pavadinimas gali apimti:
-Raidės ir skaitmenys, tačiau pirmasis simbolis negali būti skaitmuo.
-Greta raidžių naudojami simboliai`$`ir `_`.
-Leidžiami ir nelotyniški simboliai bei hieroglifai, tačiau paprastai jie nenaudojami.
Variables are dynamically typed. They can store any value:
Kintamieji yra dinamiškai tipizuojami. Juose galima saugoti bet kokią vertę:
```js
let x = 5;
x = "John";
x = "Jonas";
```
There are 8 data types:
Yra 8 duomenų tipai:
- `number`for both floating-point and integer numbers,
- `bigint`for integer numbers of arbitrary length,
- `string`for strings,
- `boolean`for logical values: `true/false`,
- `null` --a type with a single value`null`,meaning "empty" or "does not exist",
- `undefined` --a type with a single value`undefined`,meaning "not assigned",
- `object`and `symbol` --for complex data structures and unique identifiers, we haven't learnt them yet.
- `number`sveikiesiems ir realiesiems skaičiams,
- `bigint`darbui su savavališkai ilgais sveikaisiais skaičiais,
- `string`eilutėms,
- `boolean`loginėms vertėms: `true/false`,
- `null` --tipas su vienintele verte`null`,reiškiančia “tuščias” arba “neegzistuoja”,
- `undefined` --tipas su vienintele verte`undefined`,reiškiančia "nepriskirtas",
- `object`ir `symbol` --sudėtingoms duomenų struktūroms ir unikaliems identifikatoriams, jų dar nesimokome.
The `typeof`operator returns the type for a value, with two exceptions:
Operatorius `typeof`grąžina vertės tipą, išskyrus dvi išimtis:
```js
typeof null == "object" //error in the language
typeof function(){} == "function" //functions are treated specially
typeof null == "object" //kalbos klaida
typeof function(){} == "function" //funkcijos traktuojamos ypatingai
```
More in: <info:variables>and <info:types>.
Išsamiau: <info:variables>ir <info:types>.
##Interaction
##Interakcija su lankytoju
We're using a browser as a working environment, so basic UI functions will be:
Kaip darbo aplinką naudojame naršyklę, todėl pagrindinės vartotojo sąsajos funkcijos bus:
:Bitwise operators work with 32-bit integers at the lowest, bit-level: see the [docs](mdn:/JavaScript/Guide/Expressions_and_Operators#Bitwise) when they are needed.
Bitų operacijos
:Bitų operatoriai su 32 bitų sveikaisiais skaičiais dirba žemiausiu, bitų lygiu. Daugiau apie jų galite perskaityti [MDN](mdn:/JavaScript/Guide/Expressions_and_Operators#Bitwise).
Conditional
:The only operator with three parameters: `cond ? resultA : resultB`.If `cond`is truthy,returns `resultA`,otherwise `resultB`.
Sąlyginiai operatoriai
:Vienintelis operatorius, turintis tris parametrus: `cond ? resultA : resultB`.Jeigu `cond`yra truthy,grąžina `resultA`,priešingu atveju `resultB`.
Logical operators
:Logical AND `&&`and OR `||`perform short-circuit evaluation and then return the value where it stopped (not necessary`true`/`false`).Logical NOT `!`converts the operand to boolean type and returns the inverse value.
Loginiai operatoriai
:Loginiai IR `&&`ir ARBA `||`atlieka trumpąjį apdorojimą ir grąžina vertę, kurioje jie sustojo (nebūtinai`true`/`false`).Loginis NE `!`konvertuoja operandą į loginį tipą ir grąžina atvirkštinę vertę.
Nullish coalescing operator
:The `??`operator provides a way to choose a defined value from a list of variables. The result of`a ?? b`is `a` unless it's `null/undefined`,then `b`.
Nulinio susiliejimo operatorius.
:Operatorius `??`suteikia galimybę pasirinkti apibrėžtą vertę iš kintamųjų sąrašo. Rezultatas`a ?? b`yra `a`, išskyrus atvejus, kai `a` yra `null/undefined`,tada `b`.
Comparisons
:Equality check`==`for values of different types converts them to a number (except `null`and `undefined` that equal each other and nothing else),so these are equal:
Palyginimas
:Įvairių tipų verčių lygybės patikrinimas`==`paverčia jas skaičiumi (išskyrus `null`ir `undefined`, kurios lygios viena kitai ir niekam kitam),todėl toliau pateikti pavyzdžiai yra lygūs:
```js run
alert( 0 == false ); // true
alert( 0 == '' ); // true
```
Other comparisons convert to a number as well.
Kiti palyginimai taip pat konvertuojami į skaičių.
The strict equality operator`===`doesn't do the conversion: different types always mean different values for it.
Griežtosios lygybės operatorius`===`neatlieka konvertavimo: skirtingi tipai jam visada reiškia skirtingas vertes.
Values `null`and `undefined`are special: they equal `==`each other and don't equal anything else.
Vertės `null`ir `undefined`yra ypatingos: jos lygios `==`viena kitai ir nėra lygios niekam kitam.
Greater/less comparisons compare strings character-by-character, other types are converted to a number.
Daugiau/mažiau palyginimo operatoriai palygina eilutes simbolis po simbolio, kiti tipai konvertuojami į skaičius.
Other operators
:There are few others, like a comma operator.
Kiti operatoriai
:Yra keletas kitų operatorių, pavyzdžiui, kablelio operatorius.
More in: <info:operators>, <info:comparison>, <info:logical-operators>, <info:nullish-coalescing-operator>.
@@ -197,43 +197,43 @@ More in: <info:operators>, <info:comparison>, <info:logical-operators>, <info:nu
}
```
-The variable declared in`for(let...)`loop is visible only inside the loop. But we can also omit `let`and reuse an existing variable.
-Directives `break/continue`allow to exit the whole loop/current iteration. Use labels to break nested loops.
-Kintamasis, deklaruotas`for(let...)`cikle, yra matomas tik ciklo viduje. Tačiau taip pat galime praleisti `let`ir pakartotinai panaudoti egzistuojantį kintamąjį.
-Direktyvos `break/continue`leidžia išeiti iš viso ciklo/dabartinės iteracijos. Naudokite žymas norėdami išeiti iš įdėtų ciklų.
Details in: <info:while-for>.
Išsamiau: <info:while-for>.
Later we'll study more types of loops to deal with objects.
Vėliau mes nagrinėsime daugiau ciklų, skirtų darbui su objektais.
##The "switch" construct
##“switch” konstrukcija
The "switch" construct can replace multiple`if`checks. It uses `===` (strict equality) for comparisons.
Konstrukcija “switch” gali pakeisti kelis`if`patikrinimus. Palyginimui naudojama `===` (griežta lygybė).
For instance:
Pavyzdžiui:
```js run
let age = prompt('Your age?', 18);
let age = prompt('Jūsų amžius?', 18);
switch (age) {
case 18:
alert("Won't work"); //the result ofpromptis a string, not a number
alert("neveikia"); // promptrezultatas yra eilutė, o ne skaičius
break;
case "18":
alert("This works!");
alert("Tai veikia!");
break;
default:
alert("Any value not equal to one above");
alert("Bet kuri vertė, nelygi vienai iš aukščiau nurodytų verčių");
}
```
Details in: <info:switch>.
Išsamiau: <info:switch>.
##Functions
##Funkcijos
We covered three ways to create a function in #"f969d664466db81c5e117581cc79f09cf9e9b8db5e600263932e96035feb9a22">
Mes apžvelgėme tris būdus, kaip galima sukurti funkciją #"f969d664466db81c5e117581cc79f09cf9e9b8db5e600263932e96035feb9a22">
1.Function Declaration: the function in the main code flow
1.Funkcijos deklaravimas: funkcija pagrindiniame kodo sraute
```js
function sum(a, b) {
Expand All
@@ -243,7 +243,7 @@ We covered three ways to create a function in #"7320bcee6f2b951807a6b6915c38acc843c1f1083da68e347f811681b6e363c9">
}
```
2. Function Expression:the function in the context of an expression
2. Function Expression:funkcija išraiškos kontekste
```js
let sum = function(a, b) {
Expand All
@@ -253,32 +253,32 @@ We covered three ways to create a function in #"e35e98d95037b1928c27ef59c3e17bb833a180ec5973eecac8aac05b1432aff6">
//kelių eilučių kodas figūriniuose skliaustuose{ ... },čia reikalingas return:
let sum = (a, b) => {
// ...
return a + b;
}
//without arguments
//be argumentų
let sayHi = () => alert("Hello");
//with a single argument
//su vienu argumentu
let double = n => n * 2;
```
-Functions may have local variables: those declared inside its body or its parameter list. Such variables are only visible inside the function.
-Parameters can have default values: `function sum(a = 1, b = 2) {...}`.
-Functions always return something. If there's no `return` statement, then the result is `undefined`.
-Funkcijos gali turėti lokalinius kintamuosius: tuos, kurie yra deklaruoti jų viduje arba jų parametrų sąraše. Tokie kintamieji matomi tik funkcijos viduje.
-Parametrai gali turėti numatytąsias vertes: `function sum(a = 1, b = 2) {...}`.
-Funkcijos visada ką nors grąžina. Jei nėra teiginio `return`, rezultatas yra `undefined`.
Details: see <info:function-basics>, <info:arrow-functions-basics>.
That was a brief list ofJavaScriptfeatures. As of now we've studied only basics. Further in the tutorial you'll find more specials and advanced features ofJavaScript.
Tai buvo trumpasJavaScriptfunkcijų sąrašas. Kol kas susipažinome tik su pagrindais. Toliau vadovėlyje jūs rasite daugiau specialiųjų ir išplėstiniųJavaScript funkcijų.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.