Skip to content

Commit 03d9bfb

Browse files
committed
translating the Object Variable scope, closure article
1 parent 6a0335f commit 03d9bfb

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

1-js/06-advanced-functions/03-closure/article.md

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# نطاق المُتغير
21

3-
# Variable scope, closure
2+
# نطاق المتغيرات، الإغلاق
43

5-
JavaScript is a very function-oriented language. It gives us a lot of freedom. A function can be created at any moment, passed as an argument to another function, and then called from a totally different place of code later.
4+
جافا سكريبت هي لغة موجهة نحو الدوال. تمنحنا اللغة حرية كبيرة، حيث يمكن إنشاء دالة في أي وقت، وإرسالها كوسيط لدالة أخرى ومن ثم استدعائها من مكان مختلف في الكود لاحقًا.
65

7-
We already know that a function can access variables outside of it ("outer" variables).
6+
نحن بالفعل نعلم أن الدالة يمكنها الوصول إلى المتغيرات الخارجية منها ("المتغيرات الخارجية").
87

9-
But what happens if outer variables change since a function is created? Will the function get newer values or the old ones?
8+
ولكن ماذا يحدث إذا تغيرت المتغيرات الخارجية بعد إنشاء الدالة؟ هل ستحصل الدالة على القيم الأحدث أم القيم القديمة؟
109

11-
And what if a function is passed along as a parameter and called from another place of code, will it get access to outer variables at the new place?
10+
وماذا إذا تم تمرير دالة كمعلمة واستدعاؤها من مكان آخر في الكود؟ هل ستحصل الدالة على وصول إلى المتغيرات الخارجية في المكان الجديد؟
1211

13-
Let's expand our knowledge to understand these scenarios and more complex ones.
12+
دعنا نوسع معرفتنا لفهم هذه السيناريوهات والسيناريوهات الأكثر تعقيدًا.
1413

1514
سنتحدث عن المُتغيرات `let/const` هنا
1615

@@ -203,11 +202,11 @@ alert(counter()); // 2
203202

204203
# بسيط حتّى الآن، أم لا؟
205204

206-
1. When the script starts, the Lexical Environment is pre-populated with all declared variables.
207-
- Initially, they are in the "Uninitialized" state. That's a special internal state, it means that the engine knows about the variable, but it cannot be referenced until it has been declared with `let`. It's almost the same as if the variable didn't exist.
208-
2. Then `let phrase` definition appears. There's no assignment yet, so its value is `undefined`. We can use the variable from this point forward.
209-
3. `phrase` is assigned a value.
210-
4. `phrase` changes the value.
205+
1. عند بدء تشغيل البرنامج، تمتلئ البيئة اللغوية (Lexical Environment) مسبقًا بجميع المتغيرات المعلنة.
206+
- في البداية، تكون المتغيرات في الحالة "Uninitialized". هذه حالة داخلية خاصة، وتعني أن المحرك يعرف المتغير، ولكن لا يمكن الإشارة إليه حتى يتم تعريفه بـ `let`. إنها تقريبًا نفس الشيء كما لو أن المتغير لم يكن موجودًا.
207+
2. ثم يظهر تعريف `let phrase`. لا يوجد تعيين حتى الآن، لذلك قيمتها هي `undefined`. يمكننا استخدام المتغير من هذه النقطة فصاعدًا.
208+
3. يتم تعييين قيمة `phrase`.
209+
4. تتغير قيمة `phrase`.
211210

212211
- المتغير هو فعليًا خاصية لإحدى الكائنات الداخلية الخاصة، وهذا الكائن مرتبط بالكتلة أو الدالة أو السكربت الذي يجري تنفيذه حاليًا.
213212
- حين نعمل مع المتغيرات نكون في الواقع نعمل مع خصائص ذلك الكائن.
@@ -310,10 +309,7 @@ let counter = makeCounter();
310309

311310
![](closure-makecounter-nested-call.svg)
312311

313-
# الأن عندما يبدأ الكود في البحث عن المتغير `count` داخل الدالة `counter()`, يبحث أولاً في البيئة المعجمية الخاصة به وإذا كانت فارفة يبحث في البيئة المعجمية الخارجية, ثم الخارج ثم الخارج حتي يجده.
314-
315-
Now when the code inside `counter()` looks for `count` variable, it first searches its own Lexical Environment (empty, as there are no local variables there), then the Lexical Environment of the outer `makeCounter()` call, where it finds and changes it.
316-
312+
الآن عندما يبحث الكود داخل `counter()` عن متغير `count` ، يبحث أولاً في بيئته اللغوية الخاصة (التي تكون فارغة ، لأنه لا توجد متغيرات محلية هناك) ، ثم في بيئة `makeCounter()` الخارجية التي يتم استدعاؤها منها، حيث يجد المتغير ويقوم بتغييره.
317313
** المتغير تم تعديله في البيئة المعجمية حيث يعيش.**
318314

319315
ها هي الحالة بعد التنفيذ:
@@ -337,9 +333,9 @@ A [المنغلقات](https://en.wikipedia.org/wiki/Closure_(computer_programmi
337333

338334
عادةً ما تُمسح وتُحذف البيئة المُعجمية بعدما تعمل الدالة
339335

340-
However, if there's a nested function that is still reachable after the end of a function, then it has `[[Environment]]` property that references the lexical environment.
336+
ومع ذلك، إذا كان هناك دالة متداخلة يمكن الوصول إليها بعد انتهاء الدالة الأصلية، فإن لديها خاصية `[[Environment]]` التي تشير إلى البيئة اللغوية.
341337

342-
In that case the Lexical Environment is still reachable even after the completion of the function, so it stays alive.
338+
في هذه الحالة، يمكن الوصول إلى البيئة اللغوية حتى بعد اكتمال الدالة، لذلك تبقى حية.
343339

344340
مثال:
345341

@@ -356,7 +352,7 @@ let g = f(); // g.[[Environment]] stores a reference to the Lexical Environment
356352
// of the corresponding f() call
357353
```
358354

359-
Please note that if `f()` is called many times, and resulting functions are saved, then all corresponding Lexical Environment objects will also be retained in memory. In the code below, all 3 of them:
355+
يرجى ملاحظة أنه إذا تم استدعاء `f()` العديد من المرات، وتم حفظ الدوال الناتجة، فسيتم الإحتفاظ بجميع كائنات البيئة اللغوية المقابلة في الذاكرة. في الكود أدناه، سيتم الإحتفاظ بجميع الكائنات اللغوية الثلاثة:
360356

361357
```js
362358
function f() {
@@ -395,7 +391,6 @@ g = null; // ...والآن لم تعد كذلك ونكون قد نظّفنا ا
395391

396392
كما رأينا، فنظريًا طالما الدالة «حيّة تُرزق» تبقى معها كل متغيراتها الخارجية.
397393

398-
**An important side effect in V8 (Chrome, Edge, Opera) is that such variable will become unavailable in debugging.**
399394

400395
**ثمّة -في محرّك V8 (كروم وأوبرا)- تأثير مهمّ ألا وهو أنّ هذا المتغير لن يكون مُتاحًا أثناء التنقيح.**
401396

@@ -439,6 +434,6 @@ let g = f();
439434
g();
440435
```
441436

442-
This feature of V8 is good to know. If you are debugging with Chrome/Edge/Opera, sooner or later you will meet it.
437+
هذه الميزة في V8 جيدة للمعرفة. إذا كنت تقوم بتصحيح الأخطاء باستخدام Chrome / Edge / Opera ، في وقت ما ستواجه هذه الميزة.
443438

444-
That is not a bug in the debugger, but rather a special feature of V8. Perhaps it will be changed sometime. You can always check for it by running the examples on this page.
439+
لا يعتبر هذا خطأ في مصحح الأخطاء ، بل هو ميزة خاصة في V8. ربما سيتم تغييرها في وقت ما. يمكنك دائمًا التحقق من ذلك عن طريق تشغيل الأمثلة على هذه الصفحة.

0 commit comments

Comments
 (0)