Skip to content

Commit 424969c

Browse files
committed
translating the 10th task and solution of Variable scope, closure article
1 parent 03d9bfb commit 424969c

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
Let's examine what exactly happens inside `makeArmy`, and the solution will become obvious.
1+
دعنا نفحص ما يحدث بالضبط داخل `makeArmy`، وسيصبح الحل واضحًا.
22

33
1. تُنشئ مصفوفة `‎shooters‎` فارغة:
44

5-
```js
5+
````js
66
let shooters = [];
77
```
88

9-
2. Fills it with functions via `shooters.push(function)` in the loop.
9+
2. يتم ملؤها بالدوال باستخدام `shooters.push(function)` في الحلقة.
1010

1111
```js no-beautify
1212
shooters = [
@@ -43,17 +43,17 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
4343
];
4444
```
4545

46-
3. The array is returned from the function.
46+
3. يتم إرجاع المصفوفة من الدالة.
4747

48-
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.
48+
ثم في وقت لاحق، سيتم استدعاء أي عضو، على سبيل المثال `army[5]()`، وسيتم الحصول على العنصر `army[5]` من المصفوفة (وهو دالة) ويتم استدعاؤها.
4949

50-
Now why do all such functions show the same value, `10`?
50+
السؤال هو لماذا تظهر لجميع الدوال نفس القيمة، وهي الرقم `10`؟
5151

52-
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.
52+
يحدث ذلك لأنه لا يوجد متغير محلي بإسم `i` داخل دوال `shooter`. عند استدعاء مثل هذه الدالة، فإنه يتم أخذ `i` من البيئة اللغوية الخارجية.
5353

54-
Then, what will be the value of `i`?
54+
إذاً، ماهي قيمة `i`؟
5555

56-
If we look at the source:
56+
إذا نظرنا إلى الشفرة المصدرية:
5757

5858
```js
5959
function makeArmy() {
@@ -70,13 +70,13 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
7070
}
7171
```
7272

73-
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`).
74-
75-
As the result, all `shooter` functions get the same value from the outer lexical environment and that is, the last value, `i=10`.
73+
يمكننا ملاحظة أن جميع دوال `shooter` يتم إنشاؤها في البيئة اللغوية الخارجية لدالة `makeArmy()`، لكن عندما يتم استدعاء `army[5]()`، فقدانتهت دالة `makeArmy` من عملها وأصبحت قيمة `i` هي الأخيرة وهي `10` (حيث يتوقف الحلقة عند `i=10`)،
74+
75+
وبالتالي، جميع دوال `shooter` ستحصل على نفس القيمة من البيئة اللغوية الخارجية وهي القيمة الأخيرة `i=10`.
7676

7777
![](lexenv-makearmy-empty.svg)
7878

79-
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:
79+
كما ترون في الصورة أعلاه، في كل تكرار لكتلة `while {...}` يتم إنشاء بيئة لغوية جديدة. لحل هذه المشكلة، يمكننا نسخ قيمة `i` في متغير داخل كتلة `while {...}`، مثل هذا:
8080

8181
```js run
8282
function makeArmy() {
@@ -99,18 +99,18 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
9999
100100
let army = makeArmy();
101101
102-
// Now the code works correctly
102+
// الآن يعمل الكود بشكل صحيح
103103
army[0](); // 0
104104
army[5](); // 5
105105
```
106106

107-
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.
107+
هنا `let j = i` يعلن عن متغير "محلي للتكرار" `j` ويقوم بنسخ `i` فيه. القيم الأساسية تنسخ "بالقيمة"، لذلك نحصل فعليًا على نسخة مستقلة من `i` تنتمي إلى تكرار الحلقة الحالي.
108108

109-
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 the current loop iteration:
109+
يعمل الدوال `shooter` بشكل صحيح، لأن قيمة `i` تعيش الآن قليلاً أقرب. ليس في بيئة اللغة الخارجية لـ `makeArmy()`، ولكنفي البيئة اللغوية الخارجية التي تتوافق مع التكرار الحالي:
110110

111111
![](lexenv-makearmy-while-fixed.svg)
112112

113-
Such problem could also be avoided if we used `for` in the beginning, like this:
113+
كما يمكن تجنب مشكلة من هذا النوع إذا استخدمنا `for` في البداية، مثل هذا:
114114

115115
```js run demo
116116
function makeArmy() {
@@ -135,12 +135,12 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
135135
army[5](); // 5
136136
```
137137

138-
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.
138+
هذا بشكل أساسي نفس الأمر، لأن `for` في كل تكرار ينشئ بيئة لغوية جديدة، مع متغير `i` الخاص به. لذلك تشير الدالة التي تم إنشاؤها في كل تكرار إلى `i` الخاص بها، من تلك التكرار.
139139

140140
![](lexenv-makearmy-for-fixed.svg)
141141

142-
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?
142+
الآن، بعد أن قدمنا جهدًا كبيرًا في قراءة هذا الحل، وأن الوصفة النهائية هي بسيطة - استخدم `for` - قد تتساءل: هل كان ذلك يستحق كل هذا العناء؟
143143

144-
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.
144+
حسنًا، إذا كان بإمكانك الإجابة على السؤال بسهولة، فلن تقرأ الحل. لذلك، نأمل أن يكون قد ساعدك هذا السؤال على فهم الأمور بشكل أفضل.
145145

146-
Besides, there are indeed cases when one prefers `while` to `for`, and other scenarios, where such problems are real.
146+
بالإضافة إلى ذلك، ه

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ army[2](); // 10 ...and so on.
3535
*/!*
3636
```
3737

38-
Why do all of the shooters show the same value?
38+
لماذا يظهر لكل من المدفعين الرقم نفسه؟
3939

40-
Fix the code so that they work as intended.
40+
قم بإصلاح الكود حتى يعمل كما هو مفترض به.

0 commit comments

Comments
 (0)