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
Let's examine what exactly happens inside `makeArmy`, and the solution will become obvious.
2
+
Să examinăm ce se întâmplă mai exact în `makeArmy`, iar soluția va deveni evidentă.
3
3
4
-
1.It creates an empty array`shooters`:
4
+
1.Creează o matrice goală`shooters`:
5
5
6
6
```js
7
7
let shooters = [];
8
8
```
9
-
2.Fills it with functions via`shooters.push(function)`in the loop.
9
+
2.O umple cu funcții prin`shooters.push(function)`în buclă.
10
10
11
-
Every element is a function, so the resulting array looks like this:
11
+
Fiecare element este o funcție, astfel încât matricea rezultată arată astfel:
12
12
13
13
```js no-beautify
14
14
shooters = [
@@ -25,40 +25,40 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
25
25
];
26
26
```
27
27
28
-
3.The array is returned from the function.
28
+
3.Matricea este returnată din funcție.
29
29
30
-
Then, later, the call to any member, e.g. `army[5]()` will get the element `army[5]` from the array (which is a function) and calls it.
30
+
Apoi, mai târziu, apelarea oricărui membru, e.g. `army[5]()`va obține elementul `army[5]`din matrice (care este o funcție) și îl apelează.
31
31
32
-
Now why do all such functions show the same value, `10`?
32
+
Acum de ce toate aceste funcții arată aceeași valoare, `10`?
33
33
34
-
That's because there's no local variable `i` inside `shooter` functions. When such a function is called, it takes `i` from its outer lexical environment.
34
+
Asta pentru că nu există o variabilă locală `i`în interiorul funcțiilor `shooter`. Atunci când o astfel de funcție este apelată, aceasta preia`i`din mediul său lexical exterior.
35
35
36
-
Then, what will be the value of `i`?
36
+
Atunci, care va fi valoarea lui`i`?
37
37
38
-
If we look at the source:
38
+
Dacă ne uităm la sursă:
39
39
40
40
```js
41
41
function makeArmy() {
42
42
...
43
43
let i = 0;
44
44
while (i < 10) {
45
-
letshooter=function() { //shooter function
46
-
alert( i ); //should show its number
45
+
let shooter = function() { // funcția shooter
46
+
alert( i ); // ar trebui să arate numărul său
47
47
};
48
-
shooters.push(shooter); //add function to the array
48
+
shooters.push(shooter); // adaugă funcția la matrice
49
49
i++;
50
50
}
51
51
...
52
52
}
53
53
```
54
54
55
-
We can see that all`shooter`functions are created in the lexical environment of`makeArmy()`function. But when `army[5]()` is called, `makeArmy` has already finished its job, and the final value of `i` is `10` (`while` stops at `i=10`).
55
+
Putem vedea că toate funcțiile`shooter`sunt create în mediul lexical al funcției`makeArmy()`. Dar când este apelată `army[5]()`, `makeArmy`și-a terminat deja treaba, iar valoarea finală a lui`i`este`10` (`while`se oprește la`i=10`).
56
56
57
-
As the result, all `shooter` functions get the same value from the outer lexical environment and that is, the last value, `i=10`.
57
+
Ca și rezultat, toate funcțiile `shooter`obțin aceeași valoare din mediul lexical extern și anume, ultima valoare, `i=10`.
58
58
59
59

60
60
61
-
As you can see above, on each iteration of a `while {...}` block, a new lexical environment is created. So, to fix this, we can copy the value of`i`into a variable within the`while {...}` block, like this:
61
+
După cum puteți vedea mai sus, la fiecare iterație a unui bloc `while {...}`, un nou mediu lexical este creat. Așadar, pentru a remedia acest lucru, putem copia valoarea lui`i`într-o variabilă în cadrul blocului`while {...}`, astfel:
62
62
63
63
```js run
64
64
function makeArmy() {
@@ -69,8 +69,8 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
69
69
*!*
70
70
let j = i;
71
71
*/!*
72
-
letshooter=function() { //shooter function
73
-
alert( *!*j*/!* ); //should show its number
72
+
let shooter = function() { // funcția shooter
73
+
alert( *!*j*/!* ); // ar trebui să arate numărul său
74
74
};
75
75
shooters.push(shooter);
76
76
i++;
@@ -81,18 +81,18 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
81
81
82
82
let army = makeArmy();
83
83
84
-
//Now the code works correctly
84
+
// Acum codul funcționează corect
85
85
army[0](); // 0
86
86
army[5](); // 5
87
87
```
88
88
89
-
Here`let j = i`declares an "iteration-local" variable `j`and copies `i`into it. Primitives are copied "by value", so we actually get an independent copy of `i`, belonging to the current loop iteration.
89
+
Aici`let j = i`declară o variabilă `j`"de iterație locală" și copiază `i`în ea. Primitivele sunt copiate "după valoare", astfel încât obținem de fapt o copie independentă a lui `i`, aparținând iterației curente a buclei.
90
90
91
-
The shooters work correctly, because the value of `i`now lives a little bit closer. Not in `makeArmy()` Lexical Environment, but in the Lexical Environment that corresponds to the current loop iteration:
91
+
Shooters funcționează corect, deoarece valoarea lui `i`trăiește acum un pic mai aproape. Nu în mediul lexical `makeArmy()`, ci în Mediul Lexical care corespunde iterației buclei curente:
92
92
93
93

94
94
95
-
Such a problem could also be avoided if we used `for`in the beginning, like this:
95
+
O astfel de problemă ar putea fi evitată și dacă am folosi `for`la început, astfel:
96
96
97
97
```js run demo
98
98
function makeArmy() {
@@ -102,8 +102,8 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
102
102
*!*
103
103
for(let i = 0; i < 10; i++) {
104
104
*/!*
105
-
letshooter=function() { //shooter function
106
-
alert( i ); //should show its number
105
+
let shooter = function() { // funcția shooter
106
+
alert( i ); // ar trebui să arate numărul său
107
107
};
108
108
shooters.push(shooter);
109
109
}
@@ -117,13 +117,13 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
117
117
army[5](); // 5
118
118
```
119
119
120
-
That's essentially the same, because`for`on each iteration generates a new lexical environment, with its own variable`i`. So`shooter`generated in every iteration references its own `i`, from that very iteration.
120
+
În esență, este același lucru, deoarece`for`generează la fiecare iterație un nou mediu lexical, cu propria sa variabilă`i`. Astfel,`shooter`generat în fiecare iterație face referire la propriul `i`, chiar din acea iterație.
121
121
122
122

123
123
124
-
Now, as you've put so much effort into reading this, and the final recipe is so simple - just use`for`, you may wonder -- was it worth that?
124
+
Acum, având în vedere că ați depus atât de mult efort pentru a citi acest lucru, iar rețeta finală este atât de simplă-folosiți doar`for`, vă puteți întreba--s-a meritat?
125
125
126
-
Well, if you could easily answer the question, you wouldn't read the solution. So, hopefully this task must have helped you to understand things a bit better.
126
+
Ei bine, dacă ați putea răspunde cu ușurință la această întrebare, nu ați citi soluția. Așadar, sperăm că această sarcină să vă fi ajutat să înțelegeți un pic mai bine lucrurile.
127
127
128
-
Besides, there are indeed cases when one prefers`while`to `for`, and other scenarios, where such problems are real.
128
+
În rest, există într-adevăr cazuri în care se preferă`while`în locul lui `for`, precum și alte scenarii, în care astfel de probleme sunt reale.S
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/03-closure/8-filter-through-function/task.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,24 +2,24 @@ importance: 5
2
2
3
3
---
4
4
5
-
# Filter through function
5
+
# Filtrează printr-o funcție
6
6
7
-
We have a built-in method `arr.filter(f)`for arrays. It filters all elements through the function`f`. If it returns `true`, then that element is returned in the resulting array.
7
+
Avem o metodă încorporată `arr.filter(f)`pentru matrici. Aceasta filtrează toate elementele prin intermediul funcției`f`. Dacă returnează `true`, atunci elementul respectiv este returnat în matricea rezultată.
8
8
9
-
Make a set of "ready to use" filters:
9
+
Creați un set de filtre "gata de utilizare":
10
10
11
-
-`inBetween(a, b)` -- between`a`and`b`or equal to them (inclusively).
12
-
-`inArray([...])` -- in the given array.
11
+
-`inBetween(a, b)` -- între`a`și`b`sau egal cu ele (inclusiv).
12
+
-`inArray([...])` -- în matricea dată.
13
13
14
-
The usage must be like this:
14
+
Modul de utilizare trebuie să fie următorul:
15
15
16
-
-`arr.filter(inBetween(3,6))` -- selects only values between 3 and 6.
17
-
-`arr.filter(inArray([1,2,3]))` -- selects only elements matching with one of the members of`[1,2,3]`.
16
+
-`arr.filter(inBetween(3,6))` -- selectează numai valorile cuprinse între 3 și 6.
17
+
-`arr.filter(inArray([1,2,3]))` -- selectează numai elementele care se potrivesc cu unul dintre membrii din`[1,2,3]`.
0 commit comments