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
The error message in most browsers does not give us much of a clue about what went wrong.
14
+
لا تعطينا رسالة الخطأ في معظم المتصفحات الكثير من الأدلة حول الخطأ الذي حدث.
15
15
16
-
**The error appears because a semicolon is missing after`user = {...}`.**
16
+
**يظهر الخطأ لأن فاصلة منقوطة مفقودة بعد`user = {...}`.**
17
17
18
-
JavaScript does not auto-insert a semicolon before a bracket`(user.go)()`, so it reads the code like:
18
+
لا تقوم جافاسكريبت بإدراج فاصلة منقوطة تلقائيًا قبل قوس`()(user.go)`, إنها تقوم بقراءة الشيفرة هكذا:
19
19
20
20
```js no-beautify
21
21
let user = { go:... }(user.go)()
22
22
```
23
23
24
-
Then we can also see that such a joint expression is syntactically a call of the object `{ go: ... }`as a function with the argument `(user.go)`. And that also happens on the same line with `let user`, so the `user`object has not yet even been defined, hence the error.
24
+
ثم يمكننا أن نرى أيضًا أن هذا التعبير المشترك هو عبارة عن استدعاء للكائن `{ go: ... }`كتابع مع متغير `(user.go)`. وهذا يحدث أيضًا على نفس السطر مع `let user`, لذلك `user`لم يتم حتى الآن تعريف الكائن ، ومن هنا كان الخطأ.
25
25
26
-
If we insert the semicolon, all is fine:
26
+
إذا أدخلنا الفاصلة المنقوطة ، فكل شيء على ما يرام:
27
27
28
28
```js run
29
29
let user = {
@@ -34,4 +34,5 @@ let user = {
34
34
(user.go)() // John
35
35
```
36
36
37
-
Please note that parentheses around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
37
+
يرجى ملاحظة أن الأقواس حول `(user.go)` لا تفعل شيئ هنا.عادة ما يقومون بإعداد ترتيب العمليات ، ولكن هنا النقطة
38
+
`.` تعمل أولاً على أي حال, لذلك ليس هناك تأثير. فقط الشيء الفاصلة المنقوطة هو المهم.
2.The same, parentheses do not change the order of operations here, the dot is first anyway.
6
+
2.نفس الشيء ، الأقواس لا تغير ترتيب العمليات هنا ، النقطة أولاً على أي حال.
7
7
8
-
3.Here we have a more complex call `(expression).method()`. The call works as if it were split into two lines:
8
+
3.هنا لدينا تنفيذ اكثر تعقيداً `(expression).method()`. التنفيذ يعمل كما لو كان مقسوم الى سطرين:
9
9
10
10
```js no-beautify
11
-
f =obj.go; //calculate the expression
12
-
f(); //call what we have
11
+
f =obj.go; //حساب المصطلح
12
+
f(); //تنفيذ ما لدينا
13
13
```
14
14
15
-
Here`f()`is executed as a function, without `this`.
15
+
هنا`f()`يتم تنفيذها كـ تابع, بدون`this`.
16
16
17
-
4. The similar thing as `(3)`, to the left of the dot `.` we have an expression.
17
+
4.نفس الشيئ في `(3)`, ايسر النقطة `.`لدينا مصطلح.
18
18
19
-
To explain the behavior of `(3)` and `(4)` we need to recall that property accessors (dot or square brackets) return a value of the Reference Type.
19
+
لتفسير سلوك `(3)`و`(4)`نريد إعادة تنفيذ مدخلات الخاصية (نقطة او اقواس مربعة) تعيد قيمة النوع المرجعي.
20
20
21
-
Any operation on it except a method call (like assignment `=` or `||`) turns it into an ordinary value, which does not carry the information allowing to set `this`.
21
+
اى عملية عليها عدا تنفيذ التابع (مثل `=` or `||`) يحولها إلى قيمة عادية ، لا تحمل المعلومات التي تسمح بتعيينها`this`.
This article covers an advanced topic, to understand certain edge-cases better.
4
+
```warn header="خصائص متقدمه فى اللغه"
5
+
هذه المقالة تقوم بتغطية موضوع متقدم, لفهم بعض الحالات بشكل أفضل.
6
6
7
-
It's not important. Many experienced developers live fine without knowing it. Read on if you're want to know how things work under the hood.
7
+
إنها ليست مهمة. يعيش العديد من المطورين ذوي الخبرة بشكل جيد دون معرفة ذلك. تابع القراءة إذا كنت تريد معرفة كيفية عمل الأشياء خلف الكواليس.
8
8
```
9
9
10
-
A dynamically evaluated method call can lose`this`.
10
+
قد تفقد استدعاء تابع تم تقييمه بشكل ديناميكي`this`.
11
11
12
-
For instance:
12
+
علي سبيل المثال:
13
13
14
14
```js run
15
15
let user = {
@@ -18,42 +18,42 @@ let user = {
18
18
bye() { alert("Bye"); }
19
19
};
20
20
21
-
user.hi(); //works
21
+
user.hi(); //تعمل
22
22
23
-
//now let's call user.hi or user.bye depending on the name
23
+
//الآن دعونا نقوم بتشغيل user.hi أو user.bye بناءً علي الإسم
24
24
*!*
25
25
(user.name=="John"?user.hi:user.bye)(); // Error!
26
26
*/!*
27
27
```
28
28
29
-
On the last line there is a conditional operator that chooses either `user.hi`or`user.bye`. In this case the result is`user.hi`.
29
+
على السطر الأخير يوجد عامل شرطي يختار إما `user.hi`أو`user.bye`. فى هذه الحالة تكون النتيجة`user.hi`.
30
30
31
-
Then the method is immediately called with parentheses`()`. But it doesn't work correctly!
31
+
ثم يتم استدعاء التابع على الفور بين قوسين`()`. و لكنه لم يعمل بشكل صحيح!
32
32
33
-
As you can see, the call results in an error, because the value of `"this"`inside the call becomes`undefined`.
33
+
كما ترى, تشغيل التابع أحدث خطأ, بسبب أن نتيجة `"this"`داخل تشغيل التابع أنتج`undefined`.
34
34
35
-
This works (object dot method):
35
+
هذا يعمل (كائن نقطة تابع):
36
36
```js
37
37
user.hi();
38
38
```
39
39
40
-
This doesn't (evaluated method):
40
+
هذا لا يعمل:
41
41
```js
42
-
(user.name=="John"?user.hi:user.bye)(); //Error!
42
+
(user.name=="John"?user.hi:user.bye)(); //خطأ!
43
43
```
44
44
45
-
Why? If we want to understand why it happens, let's get under the hood of how `obj.method()` call works.
45
+
لماذ ؟ إذا كنا نريد ان نفهم لماذا يحدث هذا, دعونا نري خلف الكواليس كيف يعمل `()obj.method`.
46
46
47
-
## Reference type explained
47
+
## تفسير النوع المرجعي
48
48
49
-
Looking closely, we may notice two operations in `obj.method()` statement:
49
+
عند التدقيق, ربما نلاحظ وجود عمليتين علي عبارة `()obj.method`:
50
50
51
-
1.First, the dot `'.'`retrieves the property`obj.method`.
52
-
2.Then parentheses`()`execute it.
51
+
1.اولاً, النقطة `'.'`تجب الخاصية`obj.method`.
52
+
2.ثانياً الأقواس`()`تقوم بتشغيلها.
53
53
54
-
So, how does the information about`this`get passed from the first part to the second one?
54
+
لذا, كيف يمكن للمعلومات الخاصة بـ`this`ان تمر من الجزء الأول الى الجزء الثاني?
55
55
56
-
If we put these operations on separate lines, then`this`will be lost for sure:
56
+
إذا وضعنا هذه العمليات على خطوط منفصلة, اذا`this`سوف نقوم بفقدها بالتأكيد:
57
57
58
58
```js run
59
59
let user = {
@@ -62,53 +62,53 @@ let user = {
62
62
}
63
63
64
64
*!*
65
-
//split getting and calling the method in two lines
65
+
//تقسيم الحصول على واستدعاء التابع في سطرين
66
66
let hi =user.hi;
67
-
hi(); //Error, because this is undefined
67
+
hi(); //خطأ, لأن this غير معرفة
68
68
*/!*
69
69
```
70
70
71
-
Here`hi = user.hi`puts the function into the variable, and then on the last line it is completely standalone, and so there's no`this`.
71
+
هنا`hi = user.hi`يضع التابع في المتغير, ثم في السطر الأخير يكون مستقلاً تماماً, و في هذه الحالة لا يوجد`this`.
72
72
73
-
**To make `user.hi()` calls work, JavaScript uses a trick -- the dot `'.'`returns not a function, but a value of the special[Reference Type](https://tc39.github.io/ecma262/#sec-reference-specification-type).**
73
+
**لجعل `()user.hi` تعمل, جافاسكريبت تستخدم خدعة -- النقطة `'.'`لا تيعد تابع, و لكن قيمه من المميز[Reference Type](https://tc39.github.io/ecma262/#sec-reference-specification-type).**
74
74
75
-
The Reference Type is a "specification type". We can't explicitly use it, but it is used internally by the language.
75
+
النوع المرجعي هو "نوع المواصفات". لا يمكننا استخدامها صراحة, و لكن يتم استخدامها داخلياً بواسطة اللغه.
76
76
77
-
The value of Reference Type is a three-value combination `(base, name, strict)`, where:
77
+
قيمة النوع المرجعي هي مزيج من ثلاث قيم `(base, name, strict)`, حيث:
78
78
79
-
-`base`is the object.
80
-
-`name`is the property name.
81
-
-`strict`is true if`use strict`is in effect.
79
+
-`base`الكائن.
80
+
-`name`إسم الخاصية.
81
+
-`strict`تكون صحيحة اذا`use strict`تعمل.
82
82
83
-
The result of a property access `user.hi`is not a function, but a value of Reference Type. For `user.hi`in strict mode it is:
83
+
النتيجة من إستخدام `user.hi`لا يكون تابع, و لكن قيمة من النوع المرجعي. `user.hi`في الوضع الصارم تكون:
84
84
85
85
```js
86
-
//Reference Type value
86
+
//قيمة النوع المرجعي
87
87
(user, "hi", true)
88
88
```
89
89
90
-
When parentheses`()`are called on the Reference Type, they receive the full information about the object and its method, and can set the right `this` (`=user`in this case).
90
+
حيث الأقواس`()`تسمى النوع المرجعي, يتلقون المعلومات الكاملة حول الكائن و توابعه, و يمكن وضع القيمه الصحيحة لـ `this` (`=user`في هذه الحالة).
91
91
92
-
Reference type is a special "intermediary" internal type, with the purpose to pass information from dot `.`to calling parentheses`()`.
92
+
النوع المرجعي هو نوع داخلي خاص "وسيط", بغرض تمرير المعلومات من النقطة `.`الي طلب الأقواس`()`.
93
93
94
-
Any other operation like assignment `hi = user.hi`discards the reference type as a whole, takes the value of `user.hi` (a function) and passes it on. So any further operation "loses" `this`.
94
+
اى عملية اخري مثل `hi = user.hi`تتجاهل النوع المرجعي بالكامل, تأخذ القيمة من `user.hi` (التابع) و تقوم بتمريره. اذا اى من العمليات المستقبلية "تفقد" `this`.
95
95
96
-
So, as the result, the value of `this`is only passed the right way if the function is called directly using a dot `obj.method()`or square brackets`obj['method']()`syntax (they do the same here). Later in this tutorial, we will learn various ways to solve this problem such as[func.bind()](/bind#solution-2-bind).
96
+
لذا, قيمة `this`يتم تمريرها بالطريقة الصحيحة فقط إذا تم استدعاء التابع مباشرةً باستخدام نقطة `obj.method()`أو الاقواس المربعة`obj['method']()`(يقومون بنفس الوظيفه هنا). لاحقًا في هذا البرنامج التعليمي ، سنتعلم طرقًا مختلفة لحل هذه المشكلة مثل[func.bind()](/bind#solution-2-bind).
97
97
98
-
## Summary
98
+
## الملخص
99
99
100
-
Reference Type is an internal type of the language.
100
+
النوع المرجعي هو نوع داخلي من اللغة.
101
101
102
-
Reading a property, such as with dot `.`in`obj.method()`returns not exactly the property value, but a special "reference type" value that stores both the property value and the object it was taken from.
102
+
قراءة خاصية ، كما هو الحال مع النقطة `.`في`obj.method()`لا يعيد قيمة الخاصية بالضبط, و لكن "النوع المرجعي" كلاً من قيمة الخاصية والكائن الذي تم أخذها منه.
103
103
104
-
That's for the subsequent method call `()`to get the object and set`this`to it.
104
+
هذا لاستدعاء الطريقة اللاحقة `()`للوصل الى الكائن و وضع قيمة`this`بها.
105
105
106
-
For all other operations, the reference type automatically becomes the property value (a function in our case).
106
+
بالنسبة لجميع العمليات الأخرى ، يصبح النوع المرجعي تلقائيًا قيمة الخاصية (تابع في حالتنا).
107
107
108
-
The whole mechanics is hidden from our eyes. It only matters in subtle cases, such as when a method is obtained dynamically from the object, using an expression.
108
+
جميع آليات العمل مختفيه. لا يهم إلا في الحالات الدقيقة, مثل عندما يتم الحصول على طريقة ديناميكيًا من الكائن ، باستخدام تعبير.
109
109
110
110
111
111
112
112
113
113
114
-
result of dot `.`isn't actually a method, but a value of `` needs a way to pass the information about`obj`
114
+
نتيجة النقطة `.`ليست في الواقع طريقة ، ولكنها قيمة `` يحتاج إلى طريقة لتمرير المعلومات حول`obj`
0 commit comments