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
Copy file name to clipboardExpand all lines: 1-js/09-classes/06-instanceof/article.md
+84-73Lines changed: 84 additions & 73 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,62 +1,65 @@
1
-
# Class checking: "instanceof"
1
+
# فحص الأصناف عبر instanceof
2
2
3
-
The `instanceof`operator allows to check whether an object belongs to a certain class. It also takes inheritance into account.
3
+
يُتيح لنا المُعامل `instanceof`(أهو سيرورة من) فحص هل الكائن ينتمي إلى الصنف الفلاني؟ كما يأخذ الوراثة في الحسبان عند الفحص.
4
4
5
-
Such a check may be necessary in many cases. Here we'll use it for building a *polymorphic* function, the one that treats arguments differently depending on their type.
5
+
توجد حالات عديدة يكون فيها هذا الفحص ضروريًا. **سنستعمله هنا لصناعة دالة **، أي دالة تغيّر تعاملها حسب نوع الوسطاء الممرّرة لها.
6
6
7
-
## The instanceof operator [#ref-instanceof]
7
+
## معامل instanceof
8
+
9
+
صياغته هي:
8
10
9
-
The syntax is:
10
11
```js
11
12
obj instanceof Class
12
13
```
13
14
14
-
It returns `true` if `obj` belongs to the `Class` or a class inheriting from it.
15
+
يُعيد المُعامل قيمة `true` لو كان الكائن `obj` ينتمي إلى الصنف `Class` أو أيّ صنف يرثه.
16
+
17
+
مثال:
15
18
16
-
For instance:
17
19
18
20
```js run
19
21
classRabbit {}
20
22
let rabbit =newRabbit();
21
23
22
-
//is it an object of Rabbit class?
24
+
//هل هو كائن من الصنف Rabbit؟
23
25
*!*
24
-
alert( rabbit instanceof Rabbit ); //true
26
+
alert( rabbit instanceof Rabbit ); //نعم
25
27
*/!*
26
28
```
27
29
28
-
It also works with constructor functions:
30
+
كما يعمل مع الدوال البانية:
29
31
30
32
```js run
31
33
*!*
32
-
//instead of class
34
+
//بدل استعمال class
33
35
functionRabbit() {}
34
36
*/!*
35
37
36
-
alert( newRabbit() instanceof Rabbit ); //true
38
+
alert( newRabbit() instanceof Rabbit ); //نعم
37
39
```
38
40
39
-
...And with built-in classes like`Array`:
41
+
...كما والأصناف المضمّنة مثل المصفوفات`Array`:
40
42
41
43
```js run
42
44
let arr = [1, 2, 3];
43
-
alert( arr instanceofArray ); //true
44
-
alert( arr instanceofObject ); //true
45
+
alert( arr instanceofArray ); //نعم
46
+
alert( arr instanceofObject ); //نعم
45
47
```
46
48
47
-
Please note that `arr` also belongs to the `Object` class. That's because `Array` prototypically inherits from `Object`.
49
+
لاحظ كيف أنّ الكائن `arr` ينتمي إلى صنف الكائنات `Object` أيضًا، إذ ترث المصفوفات -عبر prototype- الكائناتَ.
50
+
51
+
عادةً ما يتحقّق المُعامل `instanceof` من سلسلة prototype عند الفحص. كما يمكننا وضع المنطق الذي نريد لكلّ صنف في التابِع الثابت `Symbol.hasInstance`.
48
52
49
-
Normally, `instanceof` examines the prototype chain for the check. We can also set a custom logic in the static method `Symbol.hasInstance`.
53
+
تعمل خوارزمية `obj instanceof Class` بهذه الطريقة تقريبًا:
50
54
51
-
The algorithm of `obj instanceof Class` works roughly as follows:
1. If there's a static method `Symbol.hasInstance`, then just call it: `Class[Symbol.hasInstance](obj)`. It should return either `true` or `false`, and we're done. That's how we can customize the behavior of `instanceof`.
57
+
مثال:
54
58
55
-
For example:
56
59
57
60
```js run
58
-
//setup instanceOf check that assumes that
59
-
//anything with canEat property is an animal
61
+
//ضبط instanceOf للتحقق من الافتراض القائل
62
+
//بأن كل شيء يملك الخاصية canEat هو حيوان
60
63
classAnimal {
61
64
static [Symbol.hasInstance](obj) {
62
65
if (obj.canEat) returntrue;
@@ -68,101 +71,105 @@ The algorithm of `obj instanceof Class` works roughly as follows:
68
71
alert(obj instanceof Animal); // true: Animal[Symbol.hasInstance](obj) is called
69
72
```
70
73
71
-
2. Most classes do not have `Symbol.hasInstance`. In that case, the standard logic is used:`obj instanceOf Class` checks whether `Class.prototype` is equal to one of the prototypes in the `obj` prototype chain.
74
+
2. ليس لأغلب الأصناف التابِع `Symbol.hasInstance`. في هذه الحالة تستعمل المنطق العادي: يفحص `obj instanceOf Class` لو كان كائن `Class.prototype` مساويًا لأحد كائنات prototype في سلسلة كائنات prototype للكائن `obj`.
Here's the illustration of what `rabbit instanceof Animal` compares with `Animal.prototype`:
108
+
إليك صورة توضّح طريقة موازنة`rabbit instanceof Animal`مع`Animal.prototype`:
103
109
104
110

105
111
106
-
By the way, there's also a method [objA.isPrototypeOf(objB)](mdn:js/object/isPrototypeOf), that returns`true`if`objA`is somewhere in the chain of prototypes for`objB`. So the test of`obj instanceof Class`can be rephrased as `Class.prototype.isPrototypeOf(obj)`.
112
+
كما وهناك أيضًا التابِع [objA.isPrototypeOf(objB)](mdn:js/object/isPrototypeOf)، وهو يُعيد`true`لو كان الكائن `objA`في مكان داخل سلسلة prototype للكائن `objB`. يعني أنّنا نستطيع كتابة الفحص هذا `obj instanceof Class`هكذا:`Class.prototype.isPrototypeOf(obj)`.
107
113
108
-
It's funny, but the `Class` constructor itself does not participate in the check! Only the chain of prototypes and `Class.prototype` matters.
114
+
الأمر مضحك إذ أنّ باني الصنف `Class`نفسه ليس لديه أيّ كلمة عن هذا الفحص! المهم هو سلسلة prototype وكائن `Class.prototype`فقط.
109
115
110
-
That can lead to interesting consequences when a `prototype` property is changed after the object is created.
116
+
يمكن أن يؤدّي هذا إلى عواقب مثيرة متى تغيّرت خاصية `prototype`للكائن بعد إنشائه. طالع:
111
117
112
-
Like here:
113
118
114
119
```js run
115
120
function Rabbit() {}
116
121
let rabbit = new Rabbit();
117
122
118
-
// changed the prototype
123
+
// غيّرنا كائن prototype
119
124
Rabbit.prototype = {};
120
125
121
-
// ...not a rabbit any more!
126
+
// ...لم يعد أرنبًا بعد الآن!
122
127
*!*
123
-
alert( rabbit instanceof Rabbit ); // false
128
+
alert( rabbit instanceof Rabbit ); // لا
124
129
*/!*
125
130
```
126
131
127
-
## Bonus: Object.prototype.toString for the type
132
+
## التابعObject.prototype.toStringللأنواع
128
133
129
-
We already know that plain objects are converted to string as `[object Object]`:
134
+
نعلم بأنّ الكائنات العادية حين تتحوّل إلى سلاسل نصية تصير`[object Object]`:
130
135
131
136
```js run
132
137
let obj = {};
133
138
134
139
alert(obj); // [object Object]
135
-
alert(obj.toString()); // the same
140
+
alert(obj.toString()); // كما أعلاه
136
141
```
137
142
138
-
That's their implementation of`toString`. But there's a hidden feature that makes `toString` actually much more powerful than that. We can use it as an extended `typeof` and an alternative for `instanceof`.
143
+
يعتمد هذا على طريقة توفيرهم لتنفيذ التابِع `toString`. ولكن هناك ميزة مخفيّة تجعل هذا التابِع أكثر فائدة بكثير ممّا هو عليه، أن نستعمله على أنّه مُعامل `typeof`موسّع المزايا، أو بديلًا عن التابِع `toString`.
139
144
140
-
Sounds strange? Indeed. Let's demystify.
145
+
تبدو غريبة؟ أليس كذلك؟ لنُزل الغموض.
141
146
142
-
By [specification](https://tc39.github.io/ecma262/#sec-object.prototype.tostring), the built-in `toString` can be extracted from the object and executed in the context of any other value. And its result depends on that value.
143
147
144
-
- For a number, it will be `[object Number]`
145
-
- For a boolean, it will be `[object Boolean]`
146
-
- For `null`:`[object Null]`
147
-
- For `undefined`:`[object Undefined]`
148
-
- For arrays:`[object Array]`
149
-
-...etc (customizable).
148
+
حسب [المواصفة](https://tc39.github.io/ecma262/#sec-object.prototype.tostring)، فيمكننا استخراج التابِع `toString` المضمّن من الكائن وتنفيذه في سياق أيّ قيمة أخرى نريد، وسيكون ناتجه حسب تلك القيمة.
149
+
150
+
- لو كان عددًا، فسيكون `[object Number]`
151
+
- لو كان قيمة منطقية، فسيكون `[object Boolean]`
152
+
- لو كان `null`: `[object Null]`
153
+
- لو كان `undefined`: `[object Undefined]`
154
+
- لو كانت مصفوفة:`[object Array]`
155
+
-...إلى آخره (ويمكننا تخصيص ذلك).
156
+
157
+
هيًا نوضّح:
150
158
151
-
Let's demonstrate:
152
159
153
160
```js run
154
-
// copy toString method into a variable for convenience
Here we used [call](mdn:js/function/call) as described in the chapter [](info:call-apply-decorators) to execute the function `objectToString` in the context `this=arr`.
170
+
استعملنا هنا [call](mdn:js/function/call)“ لتنفيذ الدالة `objectToString` بسياق `this=arr`.
164
171
165
-
Internally, the `toString` algorithm examines `this` and returns the corresponding result. More examples:
172
+
تفحص خوارزمية `toString` داخليًا قيمة `this` وتُعيد الناتج الموافق لها. إليك أمثلة أخرى:
0 commit comments