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
A function gets outer variables as they are now, it uses the most recent values.
3
+
O funcție primește variabilele exterioare așa cum sunt ele acum, folosește cele mai recente valori.
4
4
5
-
Old variable values are not saved anywhere. When a function wants a variable, it takes the current value from its own Lexical Environment or the outer one.
5
+
Valorile vechi ale variabilelor nu sunt salvate nicăieri. Atunci când o funcție dorește o variabilă, aceasta ia valoarea curentă din propriul mediu lexical sau din cel extern.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/03-closure/1-closure-latest-changes/task.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -2,22 +2,22 @@ importance: 5
2
2
3
3
---
4
4
5
-
# Does a function pickup latest changes?
5
+
# Preia o funcție ultimele modificări?
6
6
7
-
The function sayHi uses an external variable name. When the function runs, which value is it going to use?
7
+
Funcția sayHi folosește un nume de variabilă externă. Când funcția se execută, ce valoare va folosi?
8
8
9
9
```js
10
10
let name ="John";
11
11
12
12
functionsayHi() {
13
-
alert("Hi, "+ name);
13
+
alert("Salut, "+ name);
14
14
}
15
15
16
16
name ="Pete";
17
17
18
-
sayHi(); //what will it show: "John" or "Pete"?
18
+
sayHi(); //ce va afișa: "John" sau "Pete"?
19
19
```
20
20
21
-
Such situations are common both in browser and server-side development. A function may be scheduled to execute later than it is created, for instance after a user action or a network request.
21
+
Astfel de situații sunt frecvente atât în browser cât și în dezvoltarea pe partea de server. O funcție poate fi programată pentru a fi executată mai târziu decât este creată, de exemplu după o acțiune a utilizatorului sau după o cerere de rețea.
22
22
23
-
So, the question is: does it pick up the latest changes?
23
+
Așadar, întrebarea este: preia cele mai recente modificări?
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
The`work()`function in the code below gets`name`from the place of its origin through the outer lexical environment reference:
3
+
Funcția`work()`din codul de mai jos obține`name`din locul de origine prin intermediul referinței mediului lexical extern:
4
4
5
5

6
6
7
-
So, the result is `"Pete"`here.
7
+
Așadar, rezultatul este `"Pete"`aici.
8
8
9
-
But if there were no`let name`in`makeWorker()`, then the search would go outside and take the global variable as we can see from the chain above. In that case the result would be`"John"`.
9
+
Dar dacă nu ar exista`let name`în`makeWorker()`, atunci căutarea ar merge în exterior și ar lua variabila globală, așa cum putem vedea din lanțul de mai sus. În acest caz, rezultatul ar fi`"John"`.
0 commit comments