Skip to content

Commit ab7c263

Browse files
committed
class static properties and methods
1 parent 640a8e9 commit ab7c263

2 files changed

Lines changed: 58 additions & 31 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"workbench.colorCustomizations": {
3+
"terminal.background":"#1D2021",
4+
"terminal.foreground":"#A89984",
5+
"terminalCursor.background":"#A89984",
6+
"terminalCursor.foreground":"#A89984",
7+
"terminal.ansiBlack":"#1D2021",
8+
"terminal.ansiBlue":"#0D6678",
9+
"terminal.ansiBrightBlack":"#665C54",
10+
"terminal.ansiBrightBlue":"#0D6678",
11+
"terminal.ansiBrightCyan":"#8BA59B",
12+
"terminal.ansiBrightGreen":"#95C085",
13+
"terminal.ansiBrightMagenta":"#8F4673",
14+
"terminal.ansiBrightRed":"#FB543F",
15+
"terminal.ansiBrightWhite":"#FDF4C1",
16+
"terminal.ansiBrightYellow":"#FAC03B",
17+
"terminal.ansiCyan":"#8BA59B",
18+
"terminal.ansiGreen":"#95C085",
19+
"terminal.ansiMagenta":"#8F4673",
20+
"terminal.ansiRed":"#FB543F",
21+
"terminal.ansiWhite":"#A89984",
22+
"terminal.ansiYellow":"#FAC03B"
23+
}
24+
25+
}

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

0 commit comments

Comments
 (0)