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
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/03-closure/10-make-army/solution.md
+21-21Lines changed: 21 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
-
Let's examine what exactly happens inside`makeArmy`, and the solution will become obvious.
1
+
دعنا نفحص ما يحدث بالضبط داخل`makeArmy`، وسيصبح الحل واضحًا.
2
2
3
3
1. تُنشئ مصفوفة `shooters` فارغة:
4
4
5
-
```js
5
+
````js
6
6
let shooters = [];
7
7
```
8
8
9
-
2.Fills it with functions via `shooters.push(function)`in the loop.
9
+
2.يتم ملؤها بالدوال باستخدام `shooters.push(function)`في الحلقة.
10
10
11
11
```js no-beautify
12
12
shooters = [
@@ -43,17 +43,17 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
43
43
];
44
44
```
45
45
46
-
3.The array is returned from the function.
46
+
3.يتم إرجاع المصفوفة من الدالة.
47
47
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]`من المصفوفة (وهو دالة) ويتم استدعاؤها.
49
49
50
-
Now why do all such functions show the same value,`10`?
50
+
السؤال هو لماذا تظهر لجميع الدوال نفس القيمة، وهي الرقم`10`؟
51
51
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`من البيئة اللغوية الخارجية.
53
53
54
-
Then, what will be the value of `i`?
54
+
إذاً، ماهي قيمة `i`؟
55
55
56
-
If we look at the source:
56
+
إذا نظرنا إلى الشفرة المصدرية:
57
57
58
58
```js
59
59
function makeArmy() {
@@ -70,13 +70,13 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
70
70
}
71
71
```
72
72
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`.
76
76
77
77

78
78
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 {...}`، مثل هذا:
80
80
81
81
```js run
82
82
function makeArmy() {
@@ -99,18 +99,18 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
99
99
100
100
let army = makeArmy();
101
101
102
-
//Now the code works correctly
102
+
// الآن يعمل الكود بشكل صحيح
103
103
army[0](); // 0
104
104
army[5](); // 5
105
105
```
106
106
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` تنتمي إلى تكرار الحلقة الحالي.
108
108
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()`، ولكنفي البيئة اللغوية الخارجية التي تتوافق مع التكرار الحالي:
110
110
111
111

112
112
113
-
Such problem could also be avoided if we used`for`in the beginning, like this:
113
+
كما يمكن تجنب مشكلة من هذا النوع إذا استخدمنا`for`في البداية، مثل هذا:
114
114
115
115
```js run demo
116
116
function makeArmy() {
@@ -135,12 +135,12 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
135
135
army[5](); // 5
136
136
```
137
137
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` الخاص بها، من تلك التكرار.
139
139
140
140

141
141
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`- قد تتساءل: هل كان ذلك يستحق كل هذا العناء؟
143
143
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
+
حسنًا، إذا كان بإمكانك الإجابة على السؤال بسهولة، فلن تقرأ الحل. لذلك، نأمل أن يكون قد ساعدك هذا السؤال على فهم الأمور بشكل أفضل.
145
145
146
-
Besides, there are indeed cases when one prefers `while` to `for`, and other scenarios, where such problems are real.
0 commit comments