Skip to content

Commit 266cbe9

Browse files
committed
Translate Prototypal inheritance to AR
1 parent b115a5d commit 266cbe9

10 files changed

Lines changed: 151 additions & 135 deletions

File tree

1-js/03-code-quality/05-testing-mocha/article.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Automated testing with Mocha
1+
# الاختبار الآلي باستخدام Mocha
22

3-
Automated testing will be used in further tasks, and it's also widely used in real projects.
3+
سيتم استخدام الاختبار الآلي في مهام قادمة ، كما أنه يستخدم على نطاق واسع في المشاريع الحقيقية.
44

5-
## Why we need tests?
5+
## لماذا نحتاج إلى الاختبارات؟
66

7-
When we write a function, we can usually imagine what it should do: which parameters give which results.
7+
عندما نكتب دالة ، يمكننا عادةً تخيل ما يجب أن تفعله: أي من المعطيات تعطي النتائج.
88

9-
During development, we can check the function by running it and comparing the outcome with the expected one. For instance, we can do it in the console.
9+
أثناء التطوير ، يمكننا التحقق من الدالة عن طريق تشغيلها ومقارنة النتيجة بالنتيجة المتوقعة. على سبيل المثال ، يمكننا القيام بذلك في وحدة التحكم. (console).
1010

11-
If something is wrong -- then we fix the code, run again, check the result -- and so on till it works.
11+
إذا كان هناك شيء خاطئ - ثم نقوم بإصلاح الكود ، ونعمل مرة أخرى ، ونتحقق من النتيجة - وهكذا حتى يعمل.
1212

1313
But such manual "re-runs" are imperfect.
1414

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

2-
1. `true`, taken from `rabbit`.
3-
2. `null`, taken from `animal`.
4-
3. `undefined`, there's no such property any more.
2+
1. `true`, تأتي من `rabbit`.
3+
2. `null`, تأتي من `animal`.
4+
3. `undefined`, إذ ليس هناك خاصية بهذا الاسم بعد الآن.

1-js/08-prototypes/01-prototype-inheritance/1-property-after-delete/task.md

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

33
---
44

5-
# Working with prototype
5+
# العمل مع prototype
66

7-
Here's the code that creates a pair of objects, then modifies them.
7+
إليك شيفرة تُنشئ كائنين وتعدّلها.
8+
9+
ما القيم الّتي ستظهر في هذه العملية؟
810

9-
Which values are shown in the process?
1011

1112
```js
1213
let animal = {
@@ -28,4 +29,4 @@ delete animal.jumps;
2829
alert( rabbit.jumps ); // ? (3)
2930
```
3031

31-
There should be 3 answers.
32+
هنالك ثلاث إجابات.

1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/solution.md

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

2-
1. Let's add `__proto__`:
2+
1. لنُضيف خاصيات `__proto__`:
3+
34

45
```js run
56
let head = {
@@ -27,6 +28,7 @@
2728
alert( table.money ); // undefined
2829
```
2930

30-
2. In modern engines, performance-wise, there's no difference whether we take a property from an object or its prototype. They remember where the property was found and reuse it in the next request.
31+
2. حين نتكلّم عن المحرّكات الحديثة، فليس هناك فرق (من ناحية الأداء) لو أخذنا الخاصية من الكائن أو من النموذج الأولي، فهي تتذكّر مكان الخاصية وتُعيد استعمالها عند طلبها ثانيةً.
32+
33+
فمثلًا ستتذكّر التعليمة `pockets.glasses` بأنّها وجدت `glasses` في كائن `head`، وفي المرة التالية ستبحث هناك مباشرة. كما أنّها ذكية لتُحدّث ذاكرتها الداخلية ما إن يتغيّر شيء ما لذا فإن الأداء الأمثل في أمان.
3134

32-
For instance, for `pockets.glasses` they remember where they found `glasses` (in `head`), and next time will search right there. They are also smart enough to update internal caches if something changes, so that optimization is safe.

1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/task.md

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

33
---
44

5-
# Searching algorithm
5+
# خوارزمية بحث
66

7-
The task has two parts.
7+
ينقسم هذا التمرين إلى قسمين.
8+
9+
لديك الكائنات التالية:
810

9-
Given the following objects:
1011

1112
```js
1213
let head = {
@@ -27,5 +28,7 @@ let pockets = {
2728
};
2829
```
2930

30-
1. Use `__proto__` to assign prototypes in a way that any property lookup will follow the path: `pockets` -> `bed` -> `table` -> `head`. For instance, `pockets.pen` should be `3` (found in `table`), and `bed.glasses` should be `1` (found in `head`).
31-
2. Answer the question: is it faster to get `glasses` as `pockets.glasses` or `head.glasses`? Benchmark if needed.
31+
32+
1. استعمل `__proto__` لإسناد كائنات النموذج الأولي بحيث يكون البحث عن الخاصيات بهذه الطريقة: `pockets` ثمّ `bed` ثمّ `table` ثمّ `head` (من الأسفل إلى الأعلى على التتالي). فمثلًا، قيمة `pockets.pen` تكون `3` (من `table`)، وقيمة `bed.glasses` تكون `1` (من `head`).
33+
2. أجِب عن هذا السؤال: ما الأسرع، أن نجلب `glasses` هكذا `pockets.glasses` أم هكذا `head.glasses`؟ قِس أداء كلّ واحدة لو لزم.
34+

1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/solution.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
**The answer: `rabbit`.**
1+
**الإجابة هي: الكائن `rabbit`.**
22

3-
That's because `this` is an object before the dot, so `rabbit.eat()` modifies `rabbit`.
3+
لأنّ قيمة `this` هي الكائن قبل النقطة، بذلك يُعدّل `rabbit.eat()`.
44

5-
Property lookup and execution are two different things.
5+
عملية البحث عن الخاصيات تختلف تمامًا عن عملية تنفيذ تلك الخاصيات.
6+
7+
نجد التابِع `rabbit.eat` سيُستدعى أولًا من كائن النموذج الأولي، وبعدها نُنفّذه على أنّ `this=rabbit`.
68

7-
The method `rabbit.eat` is first found in the prototype, then executed with `this=rabbit`.

1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/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-
# Where does it write?
5+
# أين سيحدث التعديل؟
66

7-
We have `rabbit` inheriting from `animal`.
7+
لدينا الكائن `rabbit` يرث من الكائن `animal`.
8+
9+
لو استدعينا `rabbit.eat()‎` فأيّ الكائنين ستُعدل به الخاصية `full`، الكائن `animal` أم الكائن `rabbit`؟
810

9-
If we call `rabbit.eat()`, which object receives the `full` property: `animal` or `rabbit`?
1011

1112
```js
1213
let animal = {

1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
Let's look carefully at what's going on in the call `speedy.eat("apple")`.
1+
لنرى ما يحدث داخل الاستدعاء `speedy.eat("apple")` بدقّة.
22

3-
1. The method `speedy.eat` is found in the prototype (`=hamster`), then executed with `this=speedy` (the object before the dot).
3+
1. نجد التابِع `speedy.eat` في كائن النموذج الأولي الهامستر (`=hamster`)، وبعدها ننفّذه بقيمة `this=speedy` (الكائن قبل النقطة).
44

5-
2. Then `this.stomach.push()` needs to find `stomach` property and call `push` on it. It looks for `stomach` in `this` (`=speedy`), but nothing found.
5+
2. بعدها تأتي مهمة البحث للتابِع `this.stomach.push()` ليجد خاصية المعدة `stomach` ويستدعي عليها `push`. يبدأ البحث عن `stomach` في `this` (أي في `speedy`)، ولكنّه لا يجد شيئًا.
66

7-
3. Then it follows the prototype chain and finds `stomach` in `hamster`.
7+
3. بعدها يتبع سلسلة الوراثة ويجد المعدة `stomach` في `hamster`.
88

9-
4. Then it calls `push` on it, adding the food into *the stomach of the prototype*.
9+
4. ثمّ يستدعي `push` عليها ويذهب الطعام في *معدة النموذج الأولي*.
1010

11-
So all hamsters share a single stomach!
11+
بهذا تتشارك الهامسترات كلها معدةً واحدة!
1212

13-
Both for `lazy.stomach.push(...)` and `speedy.stomach.push()`, the property `stomach` is found in the prototype (as it's not in the object itself), then the new data is pushed into it.
13+
أكان `lazy.stomach.push(...)‎` أم `speedy.stomach.push()‎`، لا نجد خاصية المعدة `stomach` إلّا في كائن النموذج الأولي (إذ ليست موجودة في الكائن نفسه)، بذلك ندفع البيانات الجديدة إلى كائن النموذج الأولي.
14+
15+
لاحظ كيف أنّ هذا لا يحدث لو استعملنا طريقة الإسناد البسيط `this.stomach=‎`:
1416

15-
Please note that such thing doesn't happen in case of a simple assignment `this.stomach=`:
1617

1718
```js run
1819
let hamster = {
1920
stomach: [],
2021

2122
eat(food) {
2223
*!*
23-
// assign to this.stomach instead of this.stomach.push
24+
// نُسند إلى this.stomach بدلًا من this.stomach.push
2425
this.stomach = [food];
2526
*/!*
2627
}
@@ -34,17 +35,18 @@ let lazy = {
3435
__proto__: hamster
3536
};
3637

37-
// Speedy one found the food
38+
// وجد الهامستر السريع الطعام
3839
speedy.eat("apple");
3940
alert( speedy.stomach ); // apple
4041

41-
// Lazy one's stomach is empty
42+
// معدة ذاك الكسول فارغة
4243
alert( lazy.stomach ); // <nothing>
4344
```
4445

45-
Now all works fine, because `this.stomach=` does not perform a lookup of `stomach`. The value is written directly into `this` object.
46+
الآن يعمل كلّ شيء كما يجب، إذ لا تبحث عملية الإسناد `this.stomach=‎` عن خاصية `stomach`، بل تكتبها مباشرةً في كائن الهامستر الّذي وجد الطعام (المستدعى قبل النقطة).
47+
48+
ويمكننا تجنّب هذه المشكلة من الأساس بتخصيص معدة لكلّ هامستر (كما الطبيعي):
4649

47-
Also we can totally avoid the problem by making sure that each hamster has their own stomach:
4850

4951
```js run
5052
let hamster = {
@@ -69,12 +71,12 @@ let lazy = {
6971
*/!*
7072
};
7173

72-
// Speedy one found the food
74+
// وجد الهامستر السريع الطعام
7375
speedy.eat("apple");
7476
alert( speedy.stomach ); // apple
7577

76-
// Lazy one's stomach is empty
78+
// معدة ذاك الكسول فارغة
7779
alert( lazy.stomach ); // <nothing>
7880
```
7981

80-
As a common solution, all properties that describe the state of a particular object, like `stomach` above, should be written into that object. That prevents such problems.
82+
يكون الحلّ العام هو أن تُكتب الخاصيات كلّها الّتي تصف حالة الكائن المحدّد ذاته (مثل `stomach` أعلاه) - أن تُكتب في الكائن ذاته، وبهذا نتجنّب مشاكل تشارك المعدة.

1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/task.md

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

33
---
44

5-
# Why are both hamsters full?
5+
# لماذا أصابت التخمة كِلا الهامسترين؟
66

7-
We have two hamsters: `speedy` and `lazy` inheriting from the general `hamster` object.
7+
لدينا هامسترين، واحد سريع `speedy` وآخر كسول `lazy`، والاثنين يرثان كائن الهامستر العمومي `hamster`.
8+
9+
حين نُعطي أحدهما الطعام، نجد الآخر أُتخم أيضًا. لماذا ذلك؟ كيف نُصلح المشكلة؟
810

9-
When we feed one of them, the other one is also full. Why? How can we fix it?
1011

1112
```js run
1213
let hamster = {
@@ -25,11 +26,11 @@ let lazy = {
2526
__proto__: hamster
2627
};
2728

28-
// This one found the food
29+
// وجد هذا الهامستر الطعامَ قبل الآخر
2930
speedy.eat("apple");
3031
alert( speedy.stomach ); // apple
3132

32-
// This one also has it, why? fix please.
33+
// هذا أيضًا وجده. لماذا؟ أصلِح الشيفرة.
3334
alert( lazy.stomach ); // apple
3435
```
3536

0 commit comments

Comments
 (0)