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/01-class/article.md
+81-81Lines changed: 81 additions & 81 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,19 @@
1
1
2
-
# Class basic syntax
2
+
# الصيغة الأساسية للClass
3
3
4
4
```quote author="Wikipedia"
5
-
In object-oriented programming, a *class* is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).
5
+
في البرمجة الموجهة للكائنات ، * الفئة * عبارة عن قالب رمز برنامج قابل للتوسيع لإنشاء الكائنات ، ويوفر القيم الأولية للحالة (متغيرات الأعضاء) وتنفيذ السلوك (وظائف الأعضاء أو الأساليب).
6
6
```
7
7
8
-
In practice, we often need to create many objects of the same kind, like users, or goods or whatever.
8
+
من الناحية العملية ، نحتاج غالبًا إلى إنشاء العديد من العناصر من نفس النوع ، مثل المستخدمين أو السلع أو أيا كان.
9
9
10
-
As we already know from the chapter <info:constructor-new>, `new function` can help with that.
10
+
كما نعلم بالفعل من الفصل <info: buildor-new> ، يمكن أن تساعد `الوظيفة الجديدة` في ذلك.
11
11
12
-
But in the modern JavaScript, there's a more advanced "class" construct, that introduces great new features which are useful for object-oriented programming.
12
+
ولكن في جافا سكريبت الحديثة ، هناك بنية "صنف" أكثر تقدمًا ، تقدم ميزات جديدة رائعة مفيدة للبرمجة الموجهة للكائنات.
13
13
14
-
## The "class" syntax
14
+
#الصيغة الأساسية للClass
15
15
16
-
The basic syntax is:
16
+
الصيغة الأساسية هي:
17
17
```js
18
18
classMyClass {
19
19
// class methods
@@ -25,11 +25,11 @@ class MyClass {
25
25
}
26
26
```
27
27
28
-
Then use `new MyClass()`to create a new object with all the listed methods.
28
+
ثم استخدم `New MyClass()`لإنشاء كائن جديد بكل الطرق المدرجة.
29
29
30
-
The `constructor()`method is called automatically by `new`, so we can initialize the object there.
30
+
يتم استدعاء طريقة `constructor()`تلقائيًا بواسطة `new` ، حتى نتمكن من تهيئة الكائن هناك.
31
31
32
-
For example:
32
+
فمثلا:
33
33
34
34
```js run
35
35
classUser {
@@ -49,28 +49,28 @@ let user = new User("John");
49
49
user.sayHi();
50
50
```
51
51
52
-
When `new User("John")`is called:
53
-
1.A new object is created.
54
-
2.The `constructor` runs with the given argument and assigns `this.name` to it.
52
+
عند استدعاء`new User("John")` :
53
+
1.يتم إنشاء كائن جديد.
54
+
2.يعمل "المُنشئ" مع الوسيطة المحددة ويعين "اسم هذا" إليه.
55
55
56
-
...Then we can call object methods, such as`user.sayHi()`.
56
+
... ثم يمكننا استدعاء طرق الكائن ، مثل`user.sayHi()`.
57
57
58
58
59
-
```warn header="No comma between class methods"
60
-
A common pitfall for novice developers is to put a comma between class methods, which would result in a syntax error.
59
+
"``warn header =" لا فاصلة بين طرق الفصل "
60
+
من المشاكل الشائعة للمطورين المبتدئين وضع فاصلة بين طرق الفصل ، مما يؤدي إلى خطأ في بناء الجملة.
61
61
62
-
The notation here is not to be confused with object literals. Within the class, no commas are required.
63
-
```
62
+
لا يجب الخلط بين التدوين هنا وحرف الشيء. داخل الفصل الدراسي ، لا يلزم وجود فواصل.
63
+
``
64
64
65
-
## What is a class?
65
+
## ما هي الclass ؟
66
66
67
-
So, what exactly is a `class`? That's not an entirely new language-level entity, as one might think.
67
+
إذن ، ما هي "الطبقة" بالضبط؟ هذا ليس كيانًا جديدًا تمامًا على مستوى اللغة ، كما قد يعتقد المرء.
68
68
69
-
Let's unveil any magic and see what a class really is. That'll help in understanding many complex aspects.
69
+
دعونا نكشف عن أي سحر ونرى ما هو الصف حقا. سيساعد ذلك في فهم العديد من الجوانب المعقدة.
70
70
71
-
In JavaScript, a class is a kind of function.
71
+
في JavaScript ، الفئة هي نوع من الوظائف.
72
72
73
-
Here, take a look:
73
+
هنا ، ألق نظرة:
74
74
75
75
```js run
76
76
classUser {
@@ -84,18 +84,18 @@ alert(typeof User); // function
84
84
*/!*
85
85
```
86
86
87
-
What `class User {...}`construct really does is:
87
+
ما يفعله بناء `مستخدم الفئة {...}`حقًا هو:
88
88
89
-
1.Creates a function named `User`, that becomes the result of the class declaration. The function code is taken from the `constructor` method (assumed empty if we don't write such method).
90
-
2.Stores class methods, such as`sayHi`, in `User.prototype`.
89
+
1.إنشاء وظيفة باسم "المستخدم" ، والتي تصبح نتيجة لإعلان الفئة. يتم أخذ رمز الوظيفة من طريقة `المنشئ` (يفترض أنها فارغة إذا لم نكتب مثل هذه الطريقة).
90
+
2.يخزن طرق الفصل ، مثل`sayHi` ، في`User.prototype`.
91
91
92
-
After `new User` object is created, when we call its method, it's taken from the prototype, just as described in the chapter<info:function-prototype>. So the object has access to class methods.
92
+
بعد إنشاء كائن "مستخدم جديد" ، عندما نسمي أسلوبه ، يتم أخذه من النموذج الأولي ، تمامًا كما هو موضح في الفصل <info:function-prototype>. لذا فإن الكائن لديه حق الوصول إلى أساليب الفصل.
93
93
94
-
We can illustrate the result of `class User` declaration as:
Sometimes people say that `class` is a "syntactic sugar" (syntax that is designed to make things easier to read, but doesn't introduce anything new), because we could actually declare the same without `class` keyword at all:
121
+
أحيانًا يقول الناس أن "class" عبارة عن "سكر نحوي" (بنية تم تصميمها لتسهيل قراءة الأشياء ، ولكن لا تقدم أي شيء جديد) ، لأنه يمكننا في الواقع أن نعلن الشيء نفسه بدون كلمة "class" على الإطلاق:
122
122
123
123
```js run
124
124
// rewriting class User in pure functions
@@ -140,13 +140,13 @@ let user = new User("John");
140
140
user.sayHi();
141
141
```
142
142
143
-
The result of this definition is about the same. So, there are indeed reasons why `class` can be considered a syntactic sugar to define a constructor together with its prototype methods.
143
+
نتيجة هذا التعريف هي نفسها. لذا ، هناك بالفعل أسباب تجعل من الممكن اعتبار "class" سكرًا نحويًا لتحديد المنشئ مع طرق النموذج الأولي.
144
144
145
-
Still, there are important differences.
145
+
لا تزال هناك اختلافات مهمة.
146
146
147
-
1.First, a function created by `class` is labelled by a special internal property`[[FunctionKind]]:"classConstructor"`. So it's not entirely the same as creating it manually.
147
+
1.أولاً ، يتم تصنيف دالة تم إنشاؤها بواسطة "class" بواسطة خاصية داخلية خاصة`[[FunctionKind]]:"classConstructor"`. لذلك فهي ليست تمامًا مثل إنشائها يدويًا.
148
148
149
-
The language checks for that property in a variety of places. For example, unlike a regular function, it must be called with`new`:
149
+
تقوم اللغة بالتحقق من هذه الخاصية في أماكن متنوعة. على سبيل المثال ، على عكس الوظيفة العادية ، يجب أن يتم استدعاؤها بـ`new`:
150
150
151
151
```js run
152
152
class User {
@@ -157,7 +157,7 @@ Still, there are important differences.
157
157
User(); // Error: Class constructor User cannot be invoked without 'new'
158
158
```
159
159
160
-
Also, a string representation of a classconstructorin most JavaScript engines starts with the "class..."
160
+
أيضًا ، يبدأ تمثيل السلسلة لمنشئ فئة في معظم محركات JavaScript بـ "class..."
161
161
162
162
```js run
163
163
class User {
@@ -166,24 +166,23 @@ Still, there are important differences.
166
166
167
167
alert(User); // class User { ... }
168
168
```
169
-
There are other differences, we'll see them soon.
170
-
171
-
2. Class methods are non-enumerable.
172
-
A class definition sets `enumerable` flag to `false` for all methods in the `"prototype"`.
169
+
هناك اختلافات أخرى ، سنراها قريبًا.
173
170
174
-
That's good, because if we `for..in` over an object, we usually don't want its class methods.
171
+
2. دوال الclass لا تعد ولا تحصى.
172
+
يقوم تعريف الفئة بتعيين علامة `enumerable` إلى` false` لجميع الطرق في "" prototype "`.
175
173
176
-
3. Classes always `use strict`.
177
-
All code inside the class construct is automatically in strict mode.
174
+
هذا أمر جيد ، لأنه إذا كنا 'for..in` فوق كائن ما ، فإننا عادة لا نريد طرقه الطبقية.
178
175
179
-
Besides, `class` syntax brings many other features that we'll explore later.
176
+
3. الclasses دائمًا "استخدام صارم".
177
+
تكون جميع التعليمات البرمجية داخل بنية الفصل تلقائيًا في وضع صارم.
180
178
181
-
## Class Expression
179
+
بالإضافة إلى ذلك ، فإن بناء جملة `class` يجلب العديد من الميزات الأخرى التي سنستكشفها لاحقًا.
182
180
183
-
Just like functions, classes can be defined inside another expression, passed around, returned, assigned, etc.
181
+
## تعبير الclass
184
182
185
-
Here's an example of a class expression:
183
+
تمامًا مثل functions ، يمكن تعريف الفئات داخل تعبير آخر ، وتمريرها ، وإعادتها ، وتعيينها ، وما إلى ذلك.
186
184
185
+
إليك مثالاً على تعبير class:
187
186
```js
188
187
let User =class {
189
188
sayHi() {
@@ -192,9 +191,9 @@ let User = class {
192
191
};
193
192
```
194
193
195
-
Similar to Named Function Expressions, class expressions may have a name.
194
+
على غرار تعبيرات الوظائف المسماة ، قد يكون لتعبيرات الفئة اسم.
196
195
197
-
If a class expression has a name, it's visible inside the classonly:
196
+
إذا كان تعبير فئة له اسم ، فإنه يكون مرئيًا داخل الفصل فقط:
Class fields provide a more elegant syntax for the latter solution:
401
+
توفر حقول class بنية أكثر أناقة للحل الأخير:
402
402
403
403
```js run
404
404
classButton {
@@ -417,13 +417,13 @@ let button = new Button("hello");
417
417
setTimeout(button.click, 1000); // hello
418
418
```
419
419
420
-
The class field`click = () => {...}`creates an independent function on each `Button`object, with `this`bound to the object. Then we can pass `button.click` around anywhere, and it will be called with the right `this`.
420
+
ينشئ حقل الفئة`click = () => {...}`وظيفة مستقلة على كل كائن `Button`، مع`this`مرتبطًا بالكائن. ثم يمكننا تمرير "button.click" في أي مكان ، وسيتم استدعاؤها باستخدام `this` الصحيح.
421
421
422
-
That's especially useful in browser environment, when we need to setup a method as an event listener.
422
+
هذا مفيد بشكل خاص في بيئة المتصفح ، عندما نحتاج إلى إعداد طريقة كمستمع للأحداث.
423
423
424
-
## Summary
424
+
## ملخص
425
425
426
-
The basic class syntax looks like this:
426
+
تبدو بنية الصف الأساسية كما يلي:
427
427
428
428
```js
429
429
classMyClass {
@@ -443,6 +443,6 @@ class MyClass {
443
443
}
444
444
```
445
445
446
-
`MyClass`is technically a function (the one that we provide as `constructor`), while methods, getters and setters are written to `MyClass.prototype`.
446
+
`MyClass`هي وظيفة من الناحية الفنية (تلك التي نقدمها على أنها `مُنشئ`) ، بينما تتم كتابة الأساليب والرسومات والمستوطنين على`MyClass.prototype`.
447
447
448
-
In the next chapters we'll learn more about classes, including inheritance and other features.
448
+
في الفصول التالية سنتعلم المزيد عن الفصول ، بما في ذلك الميراث والميزات الأخرى.
0 commit comments