Skip to content

Commit b5a43a9

Browse files
committed
extending built-in classes
1 parent ab7c263 commit b5a43a9

1 file changed

Lines changed: 23 additions & 22 deletions

File tree

1-js/09-classes/05-extend-natives/article.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
# Extending built-in classes
2+
# امتداد الـ `classes` المدمجة
33

4-
Built-in classes like Array, Map and others are extendable also.
4+
الـ `classes` المدمجة مثل الـ `Array` و `Map` وغيرهم قابلين للامتداد أيضا
55

6-
For instance, here `PowerArray` inherits from the native `Array`:
6+
على سبيل المثال ، هنا يرث `PowerArray` من` Array` الأصلي:
77

88
```js run
99
// add one more method to it (can do more)
@@ -21,20 +21,20 @@ alert(filteredArr); // 10, 50
2121
alert(filteredArr.isEmpty()); // false
2222
```
2323

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` بالضبط. يستخدم التنفيذ الداخلي خاصية `مُنشئ 'الكائن لذلك.
2525

26-
In the example above,
26+
في المثال أعلاه,
2727
```js
2828
arr.constructor === PowerArray
2929
```
3030

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` بشكل أكبر على النتيجة.
3232

33-
Even more, we can customize that behavior.
33+
بل وأكثر من ذلك ، يمكننا تخصيص هذا السلوك.
3434

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 داخليًا لإنشاء كيانات جديدة في `خريطة` و` فلتر` وما إلى ذلك.
3636

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+
إذا كنا نرغب في استخدام طرق مضمنة مثل `الخريطة` أو` الفلتر` لإرجاع المصفوفات العادية ، فيمكننا إرجاع `المصفوفة` في` الرمز المحدد '، كما يلي:
3838

3939
```js run
4040
class PowerArray extends Array {
@@ -62,28 +62,29 @@ let filteredArr = arr.filter(item => item >= 10);
6262
alert(filteredArr.isEmpty()); // Error: filteredArr.isEmpty is not a function
6363
```
6464

65-
As you can see, now `.filter` returns `Array`. So the extended functionality is not passed any further.
65+
كما ترى ، الآن يُرجع `.filter`` Array`. لذلك لم يتم تمرير الدالة الموروثة أكثر من ذلك.
6666

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+
## لا وجود للتوريث الثابت في كل المدمجات
7072

71-
## No static inheritance in built-ins
73+
لها دوال ثابتة خاصة بها ، مثل `Object.keys` ،` Array.isArray` إلخ.
7274

73-
Built-in objects have their own static methods, for instance `Object.keys`, `Array.isArray` etc.
75+
كما نعلم بالفعل ، تمتد الفصول الدراسية الأصلية لبعضها البعض. على سبيل المثال ، `Array` يمتد` Object`.
7476

75-
As we already know, native classes extend each other. For instance, `Array` extends `Object`.
77+
عادة ، عندما تمد فئة واحدة أخرى ، يتم توريث كل من الأساليب الثابتة وغير الثابتة. تم شرح ذلك تمامًا في المقالة [] (info: static-properties-methods # statics-and-inheritance).
7678

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+
لكن الفصول المدمجة استثناء. لا يرثون إحصائيات عن بعضهم البعض.
7880

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 () `) طريقة ثابتة.
8082

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`:
8284

83-
Here's the picture structure for `Date` and `Object`:
8485

8586
![](object-date-inheritance.svg)
8687

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`.
8889

89-
That's an important difference of inheritance between built-in objects compared to what we get with `extends`.
90+
هذا اختلاف مهم في الميراث بين الكائنات المضمنة مقارنة بما نحصل عليه مع `extends` ''.

0 commit comments

Comments
 (0)