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
Tai veda prie labai įdomių panaudojimo būdų, lyginant su "grynu, klasikiniu, loginiu ARBA".
101
101
102
-
1.**Getting the first truthy value from a list of variables or expressions.**
102
+
1.**Gaunant pirmą truthy vertę iš kintamųjų ar išraiškų sąrašo.**
103
103
104
-
Imagine we have a list of variables which can either contain data or be `null/undefined`. How can we find the first one with data?
104
+
Įsivaizduokite, kad turime sąrašą kintamųjų, kuriuose arba yra duomenys arba `null/undefined`. Kaip mums surasti pirmąjį su duomenimis?
105
105
106
-
We can use OR`||`:
106
+
Galime naudoti ARBA`||`:
107
107
108
108
```js run
109
109
let currentUser =null;
110
110
let defaultUser ="John";
111
111
112
112
*!*
113
-
let name = currentUser || defaultUser ||"unnamed";
113
+
let name = currentUser || defaultUser ||"bevardis";
114
114
*/!*
115
115
116
-
alert( name ); //selects "John" – the first truthy value
116
+
alert( name ); //pasirenka "John" – pirmąją truthy vertę
117
117
```
118
118
119
-
If both`currentUser`and`defaultUser`were falsy, `"unnamed"` would be the result.
120
-
2.**Short-circuit evaluation.**
119
+
Jeigu abu`currentUser`ir`defaultUser`būtų falsy, rezultatas būtų `"bevardis"`.
120
+
2.**Supaprastintas įvertinimas.**
121
121
122
-
Operands can be not only values, but arbitrary expressions. OR evaluates and tests them from left to right. The evaluation stops when a truthy value is reached, and the value is returned. Thisprocess is called"a short-circuit evaluation" because it goes as short as possible from left to right.
122
+
Operandai gali būti ne tik vertės, bet ir sutartinės išraiškos. ARBA juos įvertina ir testuoja iš kairės į dešinę. Įvertinimas sustoja kai pasiekiama truthy vertė ir ta vertė sugrąžinama. Toks procesas yra vadinamas "supaprastintu įvertinimu" (ang."a short-circuit evaluation"), nes jis vyksta taip trumpai iš kairės į dešinę kaip tik įmanoma.
123
123
124
-
This is clearly seen when the expression given as the second argument has a side effect like a variable assignment.
124
+
Tai labai akivaizdu kai išraiška, duota kaip antras argumentas, turi tokį šalutinį efektą kaip kintamojo priskyrimą.
125
125
126
-
In the example below, `x`does not get assigned:
126
+
Pavyzdyje žemiau `x`nėra priskiriamas:
127
127
128
128
```js run no-beautify
129
129
let x;
130
130
131
131
*!*true*/!* || (x = 1);
132
132
133
-
alert(x); // undefined, because (x = 1) not evaluated
133
+
alert(x); // undefined, nes (x = 1) nėra įvertinamas
134
134
```
135
135
136
-
If, instead, the first argument is `false`, `||`evaluates the second one, thus running the assignment:
136
+
Tačiau jeigu pirmas argumentas yra `false`, `||`įvertina antrąjį, tada įvykdomas priskyrimas:
137
137
138
138
```js run no-beautify
139
139
let x;
@@ -143,21 +143,21 @@ Tai veda prie labai įdomių panaudojimo būdų, lyginant su "grynu, klasikiniu,
143
143
alert(x); // 1
144
144
```
145
145
146
-
An assignment is a simple case. There may be side effects, that won't show up if the evaluation doesn't reach them.
146
+
Asignavimas yra paprastas atvejis. Tam gali būti šalutinių efektų, kurie nepasirodys, jeigu įvertinimas jų nepasieks.
147
147
148
-
As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. If it's false, the second one is evaluated.
148
+
Kaip matote toks naudojimo atvejis yra "trumpesnis būdas" su `if`". Pirmasis operandas paverčiamas logine verte, antrasis įvertinamas.
149
149
150
-
Most of time, it's better to use a "regular"`if` to keep the code easy to understand, but sometimes this can be handy.
150
+
Dažniausiai, geriau naudoti "įprastinį" `if`, kad kodas būtų lengviau įskaitomas, bet kartais toks būdas gali būti naudingas.
151
151
152
-
## && (AND)
152
+
## && (IR)
153
153
154
-
The AND operator is represented with two ampersands`&&`:
154
+
IR operatorių atstovauja du ampersandai `&&`:
155
155
156
156
```js
157
157
result = a && b;
158
158
```
159
159
160
-
In classical programming, AND returns`true`if both operands are truthy and`false` otherwise:
160
+
Klasikiniame programavime, IR grąžina `true` tik tokiu atveju kai abu operandai yra arba truthy, arba `false`:
0 commit comments