Skip to content

Commit da514b6

Browse files
authored
Merge pull request #119 from mohamedElkast/master
object-methods "this"
2 parents 1494899 + 0471cdc commit da514b6

6 files changed

Lines changed: 91 additions & 120 deletions

File tree

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
**Answer: an error.**
1+
**Aالإجابة: ظهور خطأ.**
22

3-
Try it:
3+
جربها:
44
```js run
55
function makeUser() {
66
return {
@@ -11,29 +11,21 @@ function makeUser() {
1111

1212
let user = makeUser();
1313

14-
alert( user.ref.name ); // Error: Cannot read property 'name' of undefined
14+
alert( user.ref.name ); // ِلِقيمة غير معرفة 'name' خطأ: لا يمكن قراءة الخاصية
1515
```
1616

17-
That's because rules that set `this` do not look at object definition. Only the moment of call matters.
17+
ذلك لأن القواعد التي تعين `this` لا تنظر إلى تعريف الكائن. ما يهم هو وقت الاستدعاء. قيمة `this` هنا بداخل `makeUser()‎` هي `undefined`، لأنها استُدعيَت كدالة منفصلة، وليس كدالة بصياغة النقطة.
1818

19-
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax.
20-
21-
The value of `this` is one for the whole function, code blocks and object literals do not affect it.
22-
23-
So `ref: this` actually takes current `this` of the function.
24-
25-
We can rewrite the function and return the same `this` with `undefined` value:
26-
27-
```js run
19+
قيمة `this` هي واحدة للدالة ككل، ولا تؤثر عليها أجزاء الشيفرة ولا حتى الكائنات. لذا فإن `ref: this` تأخذ `this` الحالي للدالة
20+
```js runذ
2821
function makeUser(){
2922
return this; // this time there's no object literal
3023
}
3124

3225
alert( makeUser().name ); // Error: Cannot read property 'name' of undefined
3326
```
34-
As you can see the result of `alert( makeUser().name )` is the same as the result of `alert( user.ref.name )` from the previous example.
35-
36-
Here's the opposite case:
27+
كما ترى فإن نتيجة `alert( makeUser().name )` هي نفسها نتيجة `alert( user.ref.name )` من المثال السابق
28+
هنا حالة معاكسة تمامًا:
3729

3830
```js run
3931
function makeUser() {
@@ -52,4 +44,4 @@ let user = makeUser();
5244
alert( user.ref().name ); // John
5345
```
5446

55-
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
47+
أصبحت تعمل هنا لأن `user.ref()` هي دالة، وقيمة `this` تعَيَّن للكائن الذي قبل النقطة `'.'`

1-js/04-object-basics/04-object-methods/4-object-property-this/task.md

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

33
---
44

5-
# Using "this" in object literal
5+
# استخدام `this` في الكائن معرَّف باختصار عبر الأقواس
66

7-
Here the function `makeUser` returns an object.
7+
تُرجِع الدالة `makeUser` كائنًا هنا. ما النتيجة من الدخول إلى `ref` الخاص بها؟ ولماذا؟
88

99
What is the result of accessing its `ref`? Why?
1010

@@ -18,6 +18,6 @@ function makeUser() {
1818

1919
let user = makeUser();
2020

21-
alert( user.ref.name ); // What's the result?
21+
alert( user.ref.name ); // ما النتيجة؟
2222
```
2323

1-js/04-object-basics/04-object-methods/7-calculator/task.md

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

33
---
44

5-
# Create a calculator
5+
# إنشاء آلة حاسِبة
66

7-
Create an object `calculator` with three methods:
7+
أنشئ كائنًا باسم `calculator` يحوي الدوال الثلاث التالية:
88

9-
- `read()` prompts for two values and saves them as object properties.
10-
- `sum()` returns the sum of saved values.
11-
- `mul()` multiplies saved values and returns the result.
9+
- `read()` تطلب قيمتين وتحفظها كخصائص الكائن.
10+
- `sum()` تُرجِع مجموع القيم المحفوظة.
11+
- `mul()` تضرب القيم المحفوظة وتُرجِع النتيجة.
1212

1313
```js
1414
let calculator = {

1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The solution is to return the object itself from every call.
1+
الحل هو إرجاع الكائن نفسه من كل استدعاء.
22

33
```js run demo
44
let ladder = {
@@ -26,7 +26,7 @@ let ladder = {
2626
ladder.up().up().down().up().down().showStep(); // 1
2727
```
2828

29-
We also can write a single call per line. For long chains it's more readable:
29+
يمكننا أيضا كتابة استدعاء مستقل في كل سطر ليصبح سهل القراءة بالنسبة للسلاسل الأطول
3030

3131
```js
3232
ladder

1-js/04-object-basics/04-object-methods/8-chain-calls/task.md

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

33
---
44

5-
# Chaining
5+
# التسلسل
66

7-
There's a `ladder` object that allows to go up and down:
7+
لدينا الكائن `ladder` (سُلَّم) الذي يتيح الصعود والنزول:
88

99
```js
1010
let ladder = {
@@ -21,7 +21,7 @@ let ladder = {
2121
};
2222
```
2323

24-
Now, if we need to make several calls in sequence, can do it like this:
24+
الآن، إن أردنا القيام بعدة استدعاءات متتالية، يمكننا القيام بما يلي:
2525

2626
```js
2727
ladder.up();
@@ -30,10 +30,10 @@ ladder.down();
3030
ladder.showStep(); // 1
3131
```
3232

33-
Modify the code of `up`, `down` and `showStep` to make the calls chainable, like this:
33+
عَدِّل الشيفرة الخاصة بالدوال `up`، و `down`، و `showStep` لجعل الاستدعاءات متسلسلة كما يلي:
3434

3535
```js
3636
ladder.up().up().down().showStep(); // 1
3737
```
3838

39-
Such approach is widely used across JavaScript libraries.
39+
يُستخدم هذا النمط بنطاق واسع في مكتبات JavaScript

0 commit comments

Comments
 (0)