Skip to content

Commit da846e9

Browse files
Merge pull request #180
Variable scope, closure
2 parents 8d1c27b + 691200a commit da846e9

File tree

26 files changed

+272
-455
lines changed

26 files changed

+272
-455
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
The answer is: **Pete**.
1+
Răspunsul este: **Pete**.
22

3-
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.
44

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.

1-js/06-advanced-functions/03-closure/1-closure-latest-changes/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ importance: 5
22

33
---
44

5-
# Does a function pickup latest changes?
5+
# Preia o funcție ultimele modificări?
66

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?
88

99
```js
1010
let name = "John";
1111

1212
function sayHi() {
13-
alert("Hi, " + name);
13+
alert("Salut, " + name);
1414
}
1515

1616
name = "Pete";
1717

18-
sayHi(); // what will it show: "John" or "Pete"?
18+
sayHi(); // ce va afișa: "John" sau "Pete"?
1919
```
2020

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.
2222

23-
So, the question is: does it pick up the latest changes?
23+
Așadar, întrebarea este: preia cele mai recente modificări?

1-js/06-advanced-functions/03-closure/10-make-army/_js.view/solution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ function makeArmy() {
33
let shooters = [];
44

55
for(let i = 0; i < 10; i++) {
6-
let shooter = function() { // shooter function
7-
alert( i ); // should show its number
6+
let shooter = function() { // funcția shooter
7+
alert( i ); // ar trebui să arate numărul său
88
};
99
shooters.push(shooter);
1010
}

1-js/06-advanced-functions/03-closure/10-make-army/_js.view/source.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ function makeArmy() {
33

44
let i = 0;
55
while (i < 10) {
6-
let shooter = function() { // shooter function
7-
alert( i ); // should show its number
6+
let shooter = function() { // funcția shooter
7+
alert( i ); // ar trebui să arate numărul său
88
};
99
shooters.push(shooter);
1010
i++;
@@ -16,7 +16,7 @@ function makeArmy() {
1616
/*
1717
let army = makeArmy();
1818
19-
army[0](); // the shooter number 0 shows 10
20-
army[5](); // and number 5 also outputs 10...
21-
// ... all shooters show 10 instead of their 0, 1, 2, 3...
19+
army[0](); // numărul shooter 0 arată 10
20+
army[5](); // și numărul 5 de asemeni produce 10...
21+
// ... toți shooters arată 10 în loc de 0, 1, 2, 3...
2222
*/

1-js/06-advanced-functions/03-closure/10-make-army/_js.view/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ describe("army", function() {
77
window.alert = sinon.stub(window, "alert");
88
});
99

10-
it("army[0] shows 0", function() {
10+
it("army[0] arată 0", function() {
1111
army[0]();
1212
assert(alert.calledWith(0));
1313
});
1414

1515

16-
it("army[5] shows 5", function() {
16+
it("army[5] arată 5", function() {
1717
army[5]();
1818
assert(alert.calledWith(5));
1919
});
Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

2-
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ă.
33

4-
1. It creates an empty array `shooters`:
4+
1. Creează o matrice goală `shooters`:
55

66
```js
77
let shooters = [];
88
```
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ă.
1010

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:
1212

1313
```js no-beautify
1414
shooters = [
@@ -25,40 +25,40 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
2525
];
2626
```
2727

28-
3. The array is returned from the function.
28+
3. Matricea este returnată din funcție.
2929

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ă.
3131

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`?
3333

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.
3535

36-
Then, what will be the value of `i`?
36+
Atunci, care va fi valoarea lui `i`?
3737

38-
If we look at the source:
38+
Dacă ne uităm la sursă:
3939

4040
```js
4141
function makeArmy() {
4242
...
4343
let i = 0;
4444
while (i < 10) {
45-
let shooter = 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
4747
};
48-
shooters.push(shooter); // add function to the array
48+
shooters.push(shooter); // adaugă funcția la matrice
4949
i++;
5050
}
5151
...
5252
}
5353
```
5454

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`).
5656

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`.
5858

5959
![](lexenv-makearmy-empty.svg)
6060

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:
6262

6363
```js run
6464
function makeArmy() {
@@ -69,8 +69,8 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
6969
*!*
7070
let j = i;
7171
*/!*
72-
let shooter = 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
7474
};
7575
shooters.push(shooter);
7676
i++;
@@ -81,18 +81,18 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
8181
8282
let army = makeArmy();
8383
84-
// Now the code works correctly
84+
// Acum codul funcționează corect
8585
army[0](); // 0
8686
army[5](); // 5
8787
```
8888

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.
9090

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:
9292

9393
![](lexenv-makearmy-while-fixed.svg)
9494

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:
9696

9797
```js run demo
9898
function makeArmy() {
@@ -102,8 +102,8 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
102102
*!*
103103
for(let i = 0; i < 10; i++) {
104104
*/!*
105-
let shooter = 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
107107
};
108108
shooters.push(shooter);
109109
}
@@ -117,13 +117,13 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
117117
army[5](); // 5
118118
```
119119

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.
121121

122122
![](lexenv-makearmy-for-fixed.svg)
123123

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?
125125

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.
127127

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
129129

1-js/06-advanced-functions/03-closure/10-make-army/task.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@ importance: 5
22

33
---
44

5-
# Army of functions
5+
# Armata de funcții
66

7-
The following code creates an array of `shooters`.
7+
Următorul cod creează o matrice de `shooters`.
88

9-
Every function is meant to output its number. But something is wrong...
9+
Fiecare funcție este menită să emită numărul său. Dar ceva nu este în regulă...
1010

1111
```js run
1212
function makeArmy() {
1313
let shooters = [];
1414

1515
let i = 0;
1616
while (i < 10) {
17-
let shooter = function() { // create a shooter function,
18-
alert( i ); // that should show its number
17+
let shooter = function() { // crează o funcție shooter,
18+
alert( i ); // care ar trebui să arate numărul său
1919
};
20-
shooters.push(shooter); // and add it to the array
20+
shooters.push(shooter); // și adăugați-l la matrice
2121
i++;
2222
}
2323

24-
// ...and return the array of shooters
24+
// ...și returnează matricea de shooters
2525
return shooters;
2626
}
2727

2828
let army = makeArmy();
2929

3030
*!*
31-
// all shooters show 10 instead of their numbers 0, 1, 2, 3...
32-
army[0](); // 10 from the shooter number 0
33-
army[1](); // 10 from the shooter number 1
34-
army[2](); // 10 ...and so on.
31+
// toți shooters arată 10 în loc de numerele lor 0, 1, 2, 3...
32+
army[0](); // 10 de la shooter cu numărul 0
33+
army[1](); // 10 de la shooter cu numărul 1
34+
army[2](); // 10 ...și așa mai departe.
3535
*/!*
3636
```
3737

38-
Why do all of the shooters show the same value?
38+
De ce toți shooters arată aceeași valoare?
3939

40-
Fix the code so that they work as intended.
40+
Remediați codul astfel încât acestea să funcționeze așa cum a fost intenționat.
4141

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
The answer is: **Pete**.
1+
Răspunsul este: **Pete**.
22

3-
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:
44

55
![](lexenv-nested-work.svg)
66

7-
So, the result is `"Pete"` here.
7+
Așadar, rezultatul este `"Pete"` aici.
88

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"`.

1-js/06-advanced-functions/03-closure/2-closure-variable-access/task.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Which variables are available?
5+
# Ce variabile sunt disponibile?
66

7-
The function `makeWorker` below makes another function and returns it. That new function can be called from somewhere else.
7+
Funcția `makeWorker` de mai jos creează o altă funcție și o returnează. Această nouă funcție poate fi apelată din altă parte.
88

9-
Will it have access to the outer variables from its creation place, or the invocation place, or both?
9+
Va avea acces la variabilele exterioare de la locul de creare, de la locul de invocare, sau ambele?
1010

1111
```js
1212
function makeWorker() {
@@ -19,11 +19,11 @@ function makeWorker() {
1919

2020
let name = "John";
2121

22-
// create a function
22+
// creați o funcție
2323
let work = makeWorker();
2424

25-
// call it
26-
work(); // what will it show?
25+
// apelați-o
26+
work(); // ce va arăta?
2727
```
2828

29-
Which value it will show? "Pete" or "John"?
29+
Ce valoare va arăta? "Pete" sau "John"?
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
The answer: **0,1.**
1+
Răspunsul: **0,1.**
22

3-
Functions `counter` and `counter2` are created by different invocations of `makeCounter`.
3+
Funcțiile `counter` și `counter2` sunt create prin invocări diferite ale lui `makeCounter`.
44

5-
So they have independent outer Lexical Environments, each one has its own `count`.
5+
Deci ele au Medii Lexicale exterioare independente, fiecare având propriul `count`.

1-js/06-advanced-functions/03-closure/3-counter-independent/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Are counters independent?
5+
# Sunt contoarele independente?
66

7-
Here we make two counters: `counter` and `counter2` using the same `makeCounter` function.
7+
Aici facem două contoare: `counter` și `counter2` folosind aceeași funcție `makeCounter`.
88

9-
Are they independent? What is the second counter going to show? `0,1` or `2,3` or something else?
9+
Sunt ele independente? Ce va arăta cel de-al doilea contor? `0,1` sau `2,3` sau altceva?
1010

1111
```js
1212
function makeCounter() {

1-js/06-advanced-functions/03-closure/4-counter-object-independent/solution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
Surely it will work just fine.
2+
Cu siguranță că va funcționa foarte bine.
33

4-
Both nested functions are created within the same outer Lexical Environment, so they share access to the same `count` variable:
4+
Ambele funcții imbricate sunt create în cadrul aceluiași Mediu Lexical exterior, astfel încât au acces comun la aceeași variabilă `count`:
55

66
```js run
77
function Counter() {

1-js/06-advanced-functions/03-closure/4-counter-object-independent/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Counter object
5+
# Obiect contor
66

7-
Here a counter object is made with the help of the constructor function.
7+
Aici se creează un obiect contor cu ajutorul funcției constructor.
88

9-
Will it work? What will it show?
9+
Va funcționa? Ce va arăta?
1010

1111
```js
1212
function Counter() {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
The result is **an error**.
1+
Rezultatul este **o eroare**.
22

3-
The function `sayHi` is declared inside the `if`, so it only lives inside it. There is no `sayHi` outside.
3+
Funcția `sayHi` este declarată în interiorul lui `if`, deci trăiește doar în interiorul acestuia. Nu există `sayHi` în exterior.

1-js/06-advanced-functions/03-closure/5-function-in-if/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
importance: 5
22

33
---
4-
# Function in if
4+
# Funcție în if
55

6-
Look at the code. What will be the result of the call at the last line?
6+
Priviți codul. Care va fi rezultatul apelului de la ultima linie?
77

88
```js run
9-
let phrase = "Hello";
9+
let phrase = "Bună ziua";
1010

1111
if (true) {
1212
let user = "John";

0 commit comments

Comments
 (0)