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
Here `Article.compare` stands "above" articles, as a means to compare them. It's not a method of an article, but rather of the whole class.
66
+
هنا "Article.compare" تقف المقالات "أعلاه" ، كوسيلة لمقارنتها. إنها ليست دالة لـ `article` ، ولكن بدلاً من الـ `class` بأكمله.
67
67
68
-
Another example would be a so-called "factory" method. Imagine, we need few ways to create an article:
68
+
مثال آخر هو ما يسمى طريقة "المصنع". تخيل ، نحن بحاجة إلى طرق قليلة لإنشاء مقال:
69
69
70
-
1.Create by given parameters (`title`, `date` etc).
71
-
2.Create an empty article with today's date.
72
-
3. ...or else somehow.
70
+
1.إنشاء بواسطة معلمات معينة (`العنوان` ،` التاريخ` وما إلى ذلك).
71
+
2.إنشاء مقال فارغ بتاريخ اليوم.
72
+
3. ... أو بطريقة أخرى.
73
73
74
-
The first way can be implemented by the constructor. And for the second one we can make a static method of the class.
74
+
يمكن تنفيذ الطريقة الأولى من قبل المنشئ. وللثاني يمكننا عمل طريقة ثابتة للفئة.
75
75
76
-
Like`Article.createTodays()`here:
76
+
مثل`Article.createTodays()`هنا:
77
77
78
78
```js run
79
79
classArticle {
@@ -95,9 +95,9 @@ let article = Article.createTodays();
95
95
alert( article.title ); // Today's digest
96
96
```
97
97
98
-
Now every time we need to create a today's digest, we can call `Article.createTodays()`. Once again, that's not a method of an article, but a method of the whole class.
98
+
الآن في كل مرة نحتاج فيها إلى إنشاء ملخص اليوم ، يمكننا استدعاء `` Article.createTodays () `. مرة أخرى ، هذه ليست طريقة لمقالة ، ولكنها طريقة للفصل بأكمله.
99
99
100
-
Static methods are also used in database-related classes to search/save/remove entries from the database, like this:
100
+
يتم استخدام الأساليب الثابتة أيضًا في الفئات المتعلقة بقاعدة البيانات للبحث / حفظ / إزالة الإدخالات من قاعدة البيانات ، مثل هذا:
101
101
102
102
```js
103
103
// assuming Article is a special class for managing articles
@@ -109,7 +109,7 @@ Article.remove({id: 12345});
109
109
110
110
[recent browser=Chrome]
111
111
112
-
Static properties are also possible, they look like regular class properties, but prepended by`static`:
112
+
الخصائص الثابتة ممكنة أيضًا ، فهي تبدو مثل خصائص الفئة العادية ، ولكن يتم إلحاقها بـ`static`:
113
113
114
114
```js run
115
115
classArticle {
@@ -125,11 +125,11 @@ That is the same as a direct assignment to `Article`:
125
125
Article.publisher="Ilya Kantor";
126
126
```
127
127
128
-
## Inheritance of static properties and methods
128
+
## وراثة الدوال والخصائص الثابتة
129
129
130
-
Static properties and methods are inherited.
130
+
الخصائص والأساليب الثابتة موروثة.
131
131
132
-
For instance, `Animal.compare`and `Animal.planet`in the code below are inherited and accessible as `Rabbit.compare`and `Rabbit.planet`:
132
+
على سبيل المثال ، `Animal.compare`و`Animal.planet`في الشفرة أدناه موروثة ويمكن الوصول إليها باسم `Rabbit.compare`و`Rabbit.planet`:
133
133
134
134
```js run
135
135
classAnimal {
@@ -174,9 +174,9 @@ rabbits[0].run(); // Black Rabbit runs with speed 5.
174
174
alert(Rabbit.planet); // Earth
175
175
```
176
176
177
-
Now when we call `Rabbit.compare`, the inherited `Animal.compare` will be called.
177
+
الآن عندما نسمي "Rabbit.compare" ، سيتم استدعاء "Animal.compare" الموروث.
178
178
179
-
How does it work? Again, using prototypes. As you might have already guessed, `extends` gives `Rabbit` the `[[Prototype]]`reference to `Animal`.
179
+
كيف يعمل؟ مرة أخرى ، باستخدام النماذج الأولية. كما كنت قد خمنت بالفعل ، فإن كلمة "يمتد" تعطي كلمة "أرنب" يشير "[[نموذج أولي]]` إلى "حيوان".
Static methods are used for the functionality that belongs to the class "as a whole". It doesn't relate to a concrete class instance.
205
+
يتم استخدام الأساليب الثابتة للوظيفة التي تنتمي إلى الفئة "ككل". لا يتعلق الأمر بمثيل فئة ملموسة.
206
206
207
-
For example, a method for comparison`Article.compare(article1, article2)`or a factory method `Article.createTodays()`.
207
+
على سبيل المثال ، طريقة للمقارنة`Article.compare(article1 ، article2)`أو طريقة مصنع `Article.createTodays()`.
208
208
209
-
They are labeled by the word `static` in class declaration.
209
+
يتم تسميتها بكلمة "ثابت" في إعلان الفئة.
210
210
211
-
Static properties are used when we'd like to store class-level data, also not bound to an instance.
211
+
يتم استخدام الخصائص الثابتة عندما نرغب في تخزين البيانات على مستوى الفصل الدراسي ، والتي لا ترتبط أيضًا بمثيل.
212
+
213
+
الصيغة هي:
212
214
213
-
The syntax is:
214
215
215
216
```js
216
217
classMyClass {
@@ -222,13 +223,14 @@ class MyClass {
222
223
}
223
224
```
224
225
225
-
Technically, static declaration is the same as assigning to the class itself:
226
+
من الناحية الفنية ، فإن الإعلان الثابت هو نفسه التخصيص للـ `class` نفسها:
226
227
227
228
```js
228
229
MyClass.property=...
229
230
MyClass.method=...
230
231
```
231
232
232
-
Static properties and methods are inherited.
233
+
الخصائص والأساليب الثابتة موروثة.
234
+
235
+
بالنسبة إلى "الفئة B التي تمد A" ، يشير النموذج الأولي للفئة `B` نفسها إلى` A`: `B. [[Prototype]] = A`. لذا ، إذا لم يتم العثور على حقل في `B` ، فسيستمر البحث في` A`.
233
236
234
-
For `class B extends A` the prototype of the class `B` itself points to `A`: `B.[[Prototype]] = A`. So if a field is not found in `B`, the search continues in `A`.
Copy file name to clipboardExpand all lines: 1-js/09-classes/05-extend-natives/article.md
+23-22Lines changed: 23 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
2
-
# Extending built-in classes
2
+
# امتداد الـ `classes` المدمجة
3
3
4
-
Built-in classes like Array, Map and others are extendable also.
4
+
الـ `classes` المدمجة مثل الـ `Array` و `Map` وغيرهم قابلين للامتداد أيضا
5
5
6
-
For instance, here `PowerArray`inherits from the native `Array`:
6
+
على سبيل المثال ، هنا يرث `PowerArray`من`Array` الأصلي:
7
7
8
8
```js run
9
9
// add one more method to it (can do more)
@@ -21,20 +21,20 @@ alert(filteredArr); // 10, 50
21
21
alert(filteredArr.isEmpty()); // false
22
22
```
23
23
24
-
Please note a very interesting thing. Built-in methods like`filter`, `map`and others -- return new objects of exactly the inherited type `PowerArray`. Their internal implementation uses the object's `constructor` property for that.
24
+
يرجى ملاحظة شيء مثير جدا للاهتمام. الأساليب المدمجة مثل`filter` و`map`وغيرها - تُرجع كائنات جديدة من النوع الموروث`PowerArray` بالضبط. يستخدم التنفيذ الداخلي خاصية `مُنشئ 'الكائن لذلك.
25
25
26
-
In the example above,
26
+
في المثال أعلاه,
27
27
```js
28
28
arr.constructor=== PowerArray
29
29
```
30
30
31
-
When `arr.filter()`is called, it internally creates the new array of results using exactly `arr.constructor`, not basic `Array`. That's actually very cool, because we can keep using `PowerArray`methods further on the result.
31
+
عند استدعاء `arr.filter()`، فإنه ينشئ داخليًا مجموعة جديدة من النتائج باستخدام "arr.constructor" بالضبط ، وليس `Array` الأساسي. هذا في الواقع رائع جدًا ، لأنه يمكننا الاستمرار في استخدام طرق `` PowerArray` بشكل أكبر على النتيجة.
32
32
33
-
Even more, we can customize that behavior.
33
+
بل وأكثر من ذلك ، يمكننا تخصيص هذا السلوك.
34
34
35
-
We can add a special static getter `Symbol.species`to the class. If it exists, it should return the constructor that JavaScript will use internally to create new entities in `map`, `filter` and so on.
35
+
يمكننا إضافة مُصطلح ثابت خاص `Symbol.species`إلى الفصل. إذا كان موجودًا ، فيجب أن يُرجع المُنشئ الذي ستستخدمه JavaScript داخليًا لإنشاء كيانات جديدة في `خريطة` و` فلتر` وما إلى ذلك.
36
36
37
-
If we'd like built-in methods like `map` or `filter` to return regular arrays, we can return `Array` in `Symbol.species`, like here:
37
+
إذا كنا نرغب في استخدام طرق مضمنة مثل `الخريطة` أو` الفلتر` لإرجاع المصفوفات العادية ، فيمكننا إرجاع `المصفوفة` في` الرمز المحدد '، كما يلي:
alert(filteredArr.isEmpty()); // Error: filteredArr.isEmpty is not a function
63
63
```
64
64
65
-
As you can see, now`.filter` returns `Array`. So the extended functionality is not passed any further.
65
+
كما ترى ، الآن يُرجع`.filter`` Array`. لذلك لم يتم تمرير الدالة الموروثة أكثر من ذلك.
66
66
67
-
```smart header="Other collections work similarly"
68
-
Other collections, such as `Map` and `Set`, work alike. They also use `Symbol.species`.
69
-
```
67
+
"``smart header =" مجموعات أخرى تعمل بنفس الطريقة "
68
+
تعمل مجموعات أخرى ، مثل `Map` و` Set` ، على حد سواء. كما أنهم يستخدمون `Symbol.species`.
69
+
``
70
+
71
+
## لا وجود للتوريث الثابت في كل المدمجات
70
72
71
-
## No static inheritance in built-ins
73
+
لها دوال ثابتة خاصة بها ، مثل `Object.keys` ،` Array.isArray` إلخ.
72
74
73
-
Built-in objects have their own static methods, for instance `Object.keys`, `Array.isArray` etc.
75
+
كما نعلم بالفعل ، تمتد الفصول الدراسية الأصلية لبعضها البعض. على سبيل المثال ، `Array` يمتد` Object`.
74
76
75
-
As we already know, native classes extend each other. For instance, `Array` extends `Object`.
77
+
عادة ، عندما تمد فئة واحدة أخرى ، يتم توريث كل من الأساليب الثابتة وغير الثابتة. تم شرح ذلك تمامًا في المقالة [] (info: static-properties-methods # statics-and-inheritance).
76
78
77
-
Normally, when one class extends another, both static and non-static methods are inherited. That was thoroughly explained in the article [](info:static-properties-methods#statics-and-inheritance).
79
+
لكن الفصول المدمجة استثناء. لا يرثون إحصائيات عن بعضهم البعض.
78
80
79
-
But built-in classes are an exception. They don't inherit statics from each other.
81
+
على سبيل المثال ، يرث كل من `Array` و` Date` من `Object` ، لذا فإن نُسخهما تحتوي على طرق من` Object.prototype`. لكن `Array. [[Prototype]]` لا يشير إلى `Object` ، لذلك لا يوجد ، على سبيل المثال ،` Array.keys () `(أو` Date.keys () `) طريقة ثابتة.
80
82
81
-
For example, both `Array` and `Date`inherit from `Object`, so their instances have methods from `Object.prototype`. But `Array.[[Prototype]]` does not reference `Object`, so there's no, for instance, `Array.keys()` (or `Date.keys()`) static method.
83
+
إليك بنية الصورة لـ `Date`و`Object`:
82
84
83
-
Here's the picture structure for `Date` and `Object`:
84
85
85
86

86
87
87
-
As you can see, there's no link between `Date` and `Object`. They are independent, only `Date.prototype`inherits from `Object.prototype`.
88
+
كما ترى ، لا يوجد رابط بين `التاريخ` و` الكائن`. إنهم مستقلون ، فقط يرث `Date.prototype`من`Object.prototype`.
88
89
89
-
That's an important difference of inheritance between built-in objects compared to what we get with `extends`.
90
+
هذا اختلاف مهم في الميراث بين الكائنات المضمنة مقارنة بما نحصل عليه مع `extends` ''.
0 commit comments