Skip to content

Commit 4044fbe

Browse files
authored
Merge pull request #52 from Salah856/master
class static properties and methods
2 parents 4edfab1 + 897b361 commit 4044fbe

2 files changed

Lines changed: 56 additions & 53 deletions

File tree

1-js/09-classes/03-static-properties-methods/article.md

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

2-
# Static properties and methods
2+
# الخواص والدوال الثابتة
33

4-
We can also assign a method to the class function itself, not to its `"prototype"`. Such methods are called *static*.
4+
كما يمكننا تعيين خاصية لدالة الclass ذاتها, وليس لـ `"prototype"` الخاص بها. مثل هذه الدوال تسمى بـ*static*.
55

6-
In a class, they are prepended by `static` keyword, like this:
6+
في الـ class, يتم إلحاقهم بكلمة رئيسية ` static `'' ، مثل هذا:
77

88
```js run
99
class User {
@@ -17,7 +17,7 @@ class User {
1717
User.staticMethod(); // true
1818
```
1919

20-
That actually does the same as assigning it as a property directly:
20+
هذا في الواقع يفعل نفس الشيء عند تعيينه كخاصية مباشرة:
2121

2222
```js run
2323
class User { }
@@ -29,11 +29,11 @@ User.staticMethod = function() {
2929
User.staticMethod(); // true
3030
```
3131

32-
The value of `this` in `User.staticMethod()` call is the class constructor `User` itself (the "object before dot" rule).
32+
قيمة `this` في` User.staticMethod () `هي مُنشئ الفئة` المستخدم` نفسه (قاعدة "object قبل النقطة").
3333

34-
Usually, static methods are used to implement functions that belong to the class, but not to any particular object of it.
34+
عادة ، يتم استخدام الأساليب الثابتة لتنفيذ الوظائف التي تنتمي إلى الفئة ، ولكن ليس لأي object معين منها.
3535

36-
For instance, we have `Article` objects and need a function to compare them. A natural solution would be to add `Article.compare` method, like this:
36+
على سبيل المثال ، لدينا objects `Article` ونحتاج إلى وظيفة لمقارنتها. الحل الطبيعي هو إضافة طريقة `Article.compare` ، على النحو التالي:
3737

3838
```js run
3939
class Article {
@@ -63,17 +63,17 @@ articles.sort(Article.compare);
6363
alert( articles[0].title ); // CSS
6464
```
6565

66-
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` بأكمله.
6767

68-
Another example would be a so-called "factory" method. Imagine, we need few ways to create an article:
68+
مثال آخر هو ما يسمى طريقة "المصنع". تخيل ، نحن بحاجة إلى طرق قليلة لإنشاء مقال:
6969

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. ... أو بطريقة أخرى.
7373

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+
يمكن تنفيذ الطريقة الأولى من قبل المنشئ. وللثاني يمكننا عمل طريقة ثابتة للفئة.
7575

76-
Like `Article.createTodays()` here:
76+
مثل `Article.createTodays()` هنا:
7777

7878
```js run
7979
class Article {
@@ -95,9 +95,9 @@ let article = Article.createTodays();
9595
alert( article.title ); // Today's digest
9696
```
9797

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 () `. مرة أخرى ، هذه ليست طريقة لمقالة ، ولكنها طريقة للفصل بأكمله.
9999

100-
Static methods are also used in database-related classes to search/save/remove entries from the database, like this:
100+
يتم استخدام الأساليب الثابتة أيضًا في الفئات المتعلقة بقاعدة البيانات للبحث / حفظ / إزالة الإدخالات من قاعدة البيانات ، مثل هذا:
101101

102102
```js
103103
// assuming Article is a special class for managing articles
@@ -109,7 +109,7 @@ Article.remove({id: 12345});
109109

110110
[recent browser=Chrome]
111111

112-
Static properties are also possible, they look like regular class properties, but prepended by `static`:
112+
الخصائص الثابتة ممكنة أيضًا ، فهي تبدو مثل خصائص الفئة العادية ، ولكن يتم إلحاقها بـ `static`:
113113

114114
```js run
115115
class Article {
@@ -125,11 +125,11 @@ That is the same as a direct assignment to `Article`:
125125
Article.publisher = "Ilya Kantor";
126126
```
127127

128-
## Inheritance of static properties and methods
128+
## وراثة الدوال والخصائص الثابتة
129129

130-
Static properties and methods are inherited.
130+
الخصائص والأساليب الثابتة موروثة.
131131

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

134134
```js run
135135
class Animal {
@@ -174,9 +174,9 @@ rabbits[0].run(); // Black Rabbit runs with speed 5.
174174
alert(Rabbit.planet); // Earth
175175
```
176176

177-
Now when we call `Rabbit.compare`, the inherited `Animal.compare` will be called.
177+
الآن عندما نسمي "Rabbit.compare" ، سيتم استدعاء "Animal.compare" الموروث.
178178

179-
How does it work? Again, using prototypes. As you might have already guessed, `extends` gives `Rabbit` the `[[Prototype]]` reference to `Animal`.
179+
كيف يعمل؟ مرة أخرى ، باستخدام النماذج الأولية. كما كنت قد خمنت بالفعل ، فإن كلمة "يمتد" تعطي كلمة "أرنب" يشير "[[نموذج أولي]]` إلى "حيوان".
180180

181181
![](animal-rabbit-static.svg)
182182

@@ -200,17 +200,18 @@ alert(Rabbit.__proto__ === Animal); // true
200200
alert(Rabbit.prototype.__proto__ === Animal.prototype); // true
201201
```
202202

203-
## Summary
203+
## ملخص
204204

205-
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+
يتم استخدام الأساليب الثابتة للوظيفة التي تنتمي إلى الفئة "ككل". لا يتعلق الأمر بمثيل فئة ملموسة.
206206

207-
For example, a method for comparison `Article.compare(article1, article2)` or a factory method `Article.createTodays()`.
207+
على سبيل المثال ، طريقة للمقارنة `Article.compare (article1 ، article2)` أو طريقة مصنع `Article.createTodays ()`.
208208

209-
They are labeled by the word `static` in class declaration.
209+
يتم تسميتها بكلمة "ثابت" في إعلان الفئة.
210210

211-
Static properties are used when we'd like to store class-level data, also not bound to an instance.
211+
يتم استخدام الخصائص الثابتة عندما نرغب في تخزين البيانات على مستوى الفصل الدراسي ، والتي لا ترتبط أيضًا بمثيل.
212+
213+
الصيغة هي:
212214

213-
The syntax is:
214215

215216
```js
216217
class MyClass {
@@ -222,13 +223,14 @@ class MyClass {
222223
}
223224
```
224225

225-
Technically, static declaration is the same as assigning to the class itself:
226+
من الناحية الفنية ، فإن الإعلان الثابت هو نفسه التخصيص للـ `class` نفسها:
226227

227228
```js
228229
MyClass.property = ...
229230
MyClass.method = ...
230231
```
231232

232-
Static properties and methods are inherited.
233+
الخصائص والأساليب الثابتة موروثة.
234+
235+
بالنسبة إلى "الفئة B التي تمد A" ، يشير النموذج الأولي للفئة `B` نفسها إلى` A`: `B. [[Prototype]] = A`. لذا ، إذا لم يتم العثور على حقل في `B` ، فسيستمر البحث في` A`.
233236

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

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)