Skip to content

Commit 68fbd3d

Browse files
authored
Merge pull request #121 from Adham-Niazy/master
Translating Closure to Arabic
2 parents 0b61f6e + 30ea2d1 commit 68fbd3d

20 files changed

Lines changed: 305 additions & 227 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
The answer is: **Pete**.
1+
الناتج هنا هو `‎"Pete"‎`.
22

3-
A function gets outer variables as they are now, it uses the most recent values.
3+
الدالة تحضر المتغيرات الخارجية كما هي الأن إنها تستخدم التي تم تعديلها مؤخراً.
4+
5+
القديمة لا تخزن بعد الأن لذلك الدالة تحصل علي أخر تحديث للمتغير إما عن طريق البيئة المعجمية الخاصة بها أو الخارجية
46

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.

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

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

33
---
44

5-
# Does a function pickup latest changes?
5+
# هل تلتقط الدالة أخر التغييرات؟
6+
7+
الدالة sayHi تستخدم اسم مُتغير خارجي .
8+
عندما تعمل الادلة أي منهم سيستخدم؟
69

7-
The function sayHi uses an external variable name. When the function runs, which value is it going to use?
810

911
```js
1012
let name = "John";
@@ -18,6 +20,7 @@ name = "Pete";
1820
sayHi(); // what will it show: "John" or "Pete"?
1921
```
2022

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.
23+
مواقف كهذه تعتبر شائعة إما بالنسبة غلي المتصفح أو الخادم. الادلة يمكن أن تتم مناداتها لاحقاً مثلاً بعد القيام ببعض الخطوات.
24+
25+
إذا السؤال هو هل ستشعر الدالة بالتغيير؟
2226

23-
So, the question is: does it pick up the latest changes?

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11

2-
Let's examine what's done inside `makeArmy`, and the solution will become obvious.
2+
لنُجري مسحًا شاملًا على ما يجري في `makeArmy`، حينها يظهر لنا الحل جليًا.
33

4-
1. It creates an empty array `shooters`:
4+
1. تُنشئ مصفوفة `shooters` فارغة:
55

66
```js
77
let shooters = [];
88
```
9-
2. Fills it in the loop via `shooters.push(function...)`.
9+
. تملأ المصفوفة في حلقة عبر `‎shooters.push(function...)‎`.
10+
11+
كلّ عنصر هو دالة، بهذا تكون المصفوفة الناتجة هكذا:
1012

11-
Every element is a function, so the resulting array looks like this:
1213

1314
```js no-beautify
1415
shooters = [
@@ -25,17 +26,18 @@ Let's examine what's done inside `makeArmy`, and the solution will become obviou
2526
];
2627
```
2728

28-
3. The array is returned from the function.
29+
3. تُعيد الدالة المصفوفة.
30+
31+
لاحقًا، يستلم استدعاء `‎army[5]()‎` العنصر `‎army[5]‎` من المصفوفة، وهي دالة فيستدعيها.
2932

30-
Then, later, the call to `army[5]()` will get the element `army[5]` from the array (it will be a function) and call it.
33+
الآن، لماذا تعرض كلّ هذه الدوال نفس الناتج؟
3134

32-
Now why all such functions show the same?
35+
يعزو ذلك إلى عدم وجود أيّ متغير محلي باسم `‎i‎` في دوال `‎shooter‎`. فحين تُستدعى هذه الدالة تأخذ المتغير `‎i‎` من البيئة المُعجمية الخارجية.
3336

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.
37+
وماذا ستكون قيمة `‎i‎`؟
3538

36-
What will be the value of `i`?
39+
لو رأينا مصدر القيمة:
3740

38-
If we look at the source:
3941

4042
```js
4143
function makeArmy() {
@@ -50,12 +52,12 @@ function makeArmy() {
5052
...
5153
}
5254
```
55+
كما نرى... «تعيش» القيمة في البيئة المُعجمية المرتبطة بدورة `‎makeArmy()‎` الحالية. ولكن متى استدعينا `‎army[5]()‎`، تكون دالة `‎makeArmy‎` قد أنهت مهمّتها فعلًا وقيمة `‎i‎` هي آخر قيمة، أي `‎10‎` (قيمة نهاية حلقة `‎while‎`).
5356

54-
...We can see that it lives in the lexical environment associated with the current `makeArmy()` run. But when `army[5]()` is called, `makeArmy` has already finished its job, and `i` has the last value: `10` (the end of `while`).
57+
وبهذا تأخذ كلّ دوال `‎shooter‎` القيمة من البيئة المُعجمية الخارجية، ذات القيمة الأخيرة `‎i=10‎`.
5558

56-
As a result, all `shooter` functions get from the outer lexical envrironment the same, last value `i=10`.
59+
يمكن أن نُصلح ذلك بنقل تعريف المتغير إلى داخل الحلقة:
5760

58-
We can fix it by moving the variable definition into the loop:
5961

6062
```js run demo
6163
function makeArmy() {
@@ -65,8 +67,8 @@ function makeArmy() {
6567
*!*
6668
for(let i = 0; i < 10; i++) {
6769
*/!*
68-
let shooter = function() { // shooter function
69-
alert( i ); // should show its number
70+
let shooter = function() { // دالة مُطلق النار
71+
alert( i ); // المفترض أن ترينا رقمها
7072
};
7173
shooters.push(shooter);
7274
}
@@ -80,15 +82,17 @@ army[0](); // 0
8082
army[5](); // 5
8183
```
8284

83-
Now it works correctly, because every time the code block in `for (let i=0...) {...}` is executed, a new Lexical Environment is created for it, with the corresponding variable `i`.
85+
الآن صارت تعمل كما يجب إذ في كلّ مرة تُنفّذ كتلة الشيفرة في `‎for (let i=0...) {...}‎`، يُنشئ المحرّك بيئة مُعجمية جديدة لها فيها متغير `‎i‎` المناسب لتلك الكتلة.
86+
87+
إذًا لنلخّص: قيمة `‎i‎` صارت «تعيش» أقرب للدالة من السابق. لم تعد في بيئة `‎makeArmy()‎` المُعجمية بل الآن في تلك البيئة المخصّصة لدورة الحلقة الحالية. هكذا صارت تعمل كما يجب.
8488

85-
So, 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. That's why now it works.
8689

8790
![](lexenv-makearmy.svg)
8891

89-
Here we rewrote `while` into `for`.
92+
أعدنا كتابة الشيفرة هنا وعوّضنا `‎while‎` بحلقة `‎for‎`.
93+
94+
يمكننا أيضًا تنفيذ حيلة أخرى. لنراها لنفهم الموضوع أكثر:
9095

91-
Another trick could be possible, let's see it for better understanding of the subject:
9296

9397
```js run
9498
function makeArmy() {
@@ -99,8 +103,8 @@ function makeArmy() {
99103
*!*
100104
let j = i;
101105
*/!*
102-
let shooter = function() { // shooter function
103-
alert( *!*j*/!* ); // should show its number
106+
let shooter = function() { // دالة مُطلق النار
107+
alert( *!*j*/!* ); // (*) المفترض أن ترينا رقمها
104108
};
105109
shooters.push(shooter);
106110
i++;
@@ -115,6 +119,7 @@ army[0](); // 0
115119
army[5](); // 5
116120
```
117121

118-
The `while` loop, just like `for`, makes a new Lexical Environment for each run. So here we make sure that it gets the right value for a `shooter`.
122+
كما حلقة `‎for‎`، فحلقة `‎while‎` تصنع بيئة مُعجمية جديدة لكلّ دورة، وهكذا نتأكّد بأن تكون قيمة `‎shooter‎` صحيحة.
123+
124+
باختصار ننسخ القيمة `‎let j = i‎` وهذا يصنع المتغير `‎j‎` المحلي داخل الحلقة وينسخ قيمة `‎i‎` إلى نفسه. تُنسخ الأنواع الأولية «حسب قيمتها» _By value_، لذا بهذا نأخذ نسخة كاملة مستقلة تمامًا عن `‎i‎`، ولكنّها مرتبطة بالدورة الحالية في الحلقة.
119125

120-
We copy `let j = i`. This makes a loop body local `j` and copies the value of `i` to it. Primitives are copied "by value", so we actually get a complete independent copy of `i`, belonging to the current loop iteration.

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

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

33
---
44

5-
# Army of functions
5+
### جيش من الدوال
66

7-
The following code creates an array of `shooters`.
7+
تصنع الشيفرة الآتية مصفوفة من مُطلقي النار `shooters`.
88

9-
Every function is meant to output its number. But something is wrong...
9+
يفترض أن تكتب لنا كلّ دالة رقم هويّتها، ولكن ثمّة خطب فيها...
1010

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

1515
let i = 0;
1616
while (i < 10) {
17-
let shooter = function() { // shooter function
18-
alert( i ); // should show its number
17+
let shooter = function() { // دالة مُطلق النارn
18+
alert( i ); // المفترض أن ترينا رقمها
1919
};
2020
shooters.push(shooter);
2121
i++;
@@ -26,10 +26,11 @@ function makeArmy() {
2626

2727
let army = makeArmy();
2828

29-
army[0](); // the shooter number 0 shows 10
30-
army[5](); // and number 5 also outputs 10...
31-
// ... all shooters show 10 instead of their 0, 1, 2, 3...
29+
army[0](); // مُطلق النار بالهويّة 0 يقول أنّه 10
30+
army[5](); // ‫مُطلق النار بالهويّة 5 يقول أنّه 10...
31+
// ... كلّ مُطلقي النار يقولون 10 بدل هويّاتهم 0 فَـ 1 فَـ 2 فَـ 3...
32+
3233
```
3334

34-
Why do all of the shooters show the same value? Fix the code so that they work as intended.
35+
لماذا هويّة كلّ مُطلق نار نفس البقية؟ أصلِح الشيفرة لتعمل كما ينبغي أن تعمل.
3536

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
The answer is: **Pete**.
1+
أفترض الآن بأنّ إجابة السؤال الثاني في أول الفصل ستكون جليّة.
2+
3+
دالة `‎work()‎` في الشيفرة أدناه تأخذ الاسم `‎name‎` من مكانه الأصل عبر إشارة البيئة المُعجمية الخارجية إليه:
24

3-
The `work()` function in the code below gets `name` from the place of its origin through the outer lexical environment reference:
45

56
![](lexenv-nested-work.svg)
67

7-
So, the result is `"Pete"` here.
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+
إذًا، فالناتج هنا هو `‎"Pete"‎`.
10+
11+
ولكن لو لم نكتب `‎let name‎` في `‎makeWorker()‎` فسينتقل البحث إلى خارج الدالة تلك ويأخذ القيمة العمومية كما نرى من السلسلة أعلاه. في تلك الحالة سيكون الناتج `‎"John"‎`.
12+

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

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

33
---
44

5-
# Which variables are available?
5+
# أي من المُتغيرات متاح؟
66

7-
The function `makeWorker` below makes another function and returns it. That new function can be called from somewhere else.
7+
الدالة `makeWorker` بالإسفل تصنع دالة أخري وتعيدها. هذه الدالة المُعادة يمكن مناداتها من أي مكان.
8+
9+
هل ستحصل علي حق الوصول إلي المتغيرات الخارجية من موقع بنائها أم من موقع مناداتها أو من الاثنين؟
810

9-
Will it have access to the outer variables from its creation place, or the invocation place, or both?
1011

1112
```js
1213
function makeWorker() {
@@ -25,5 +26,5 @@ let work = makeWorker();
2526
// call it
2627
work(); // what will it show?
2728
```
29+
أي قيمة سوف تظهر؟ "Pete" أم "John"؟
2830

29-
Which value it will show? "Pete" or "John"?

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
The answer: **0,1.**
1+
الإجابة هي: **0,1**.
22

3-
Functions `counter` and `counter2` are created by different invocations of `makeCounter`.
3+
صنعنا الدالتين `‎counter‎` و `‎counter2‎` باستدعاءين `‎makeCounter‎` مختلفين تمامًا.
4+
5+
لذا فلكلّ منهما بيئات مُعجمية خارجية مستقلة عن بعضها، ولكلّ منهما متغير `‎count‎` مستقل عن الثاني.
46

5-
So they have independent outer Lexical Environments, each one has its own `count`.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
importance: 5
1+
_الأهمية: 5_
22

33
---
44

5-
# Are counters independent?
5+
### هل العدّادات مستقلة عن بعضها البعض؟
66

7-
Here we make two counters: `counter` and `counter2` using the same `makeCounter` function.
7+
صنعنا هنا عدّادين اثنين `counter` و `counter2` باستعمال ذات الدالة `makeCounter`.
88

9-
Are they independent? What is the second counter going to show? `0,1` or `2,3` or something else?
9+
هل هما مستقلان عن بعضهما البعض؟ ما الذي سيعرضه العدّاد الثاني؟ `0,1` أم `2,3` أم ماذا؟
1010

1111
```js
1212
function makeCounter() {

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

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

2-
Surely it will work just fine.
2+
طبعًا، ستعمل كما يجب.
3+
4+
صُنعت الدالتين المتداخلتين في نفس البيئة المُعجمية الخارجية، بهذا تتشاركان نفس المتغير `‎count‎` وتصلان إليه:
35

4-
Both nested functions are created within the same outer Lexical Environment, so they share access to the same `count` variable:
56

67
```js run
78
function Counter() {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
importance: 5
1+
_الأهمية: 5_
22

33
---
44

5-
# Counter object
5+
### كائن عد
66

7-
Here a counter object is made with the help of the constructor function.
7+
هنا صنعنا كائن عدّ بمساعدة دالة مُنشئة _Constructor Function_.
8+
9+
هل ستعمل؟ ماذا سيظهر؟
810

9-
Will it work? What will it show?
1011

1112
```js
1213
function Counter() {

0 commit comments

Comments
 (0)