Skip to content

Commit d3cf00f

Browse files
committed
Class basic syntax
1 parent 5f8472a commit d3cf00f

2 files changed

Lines changed: 85 additions & 85 deletions

File tree

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
importance: 5
1+
درجة الأهمية : 5
22

33
---
44

5-
# Rewrite to class
5+
# أعد صياغة الclass
66

7-
The `Clock` class is written in functional style. Rewrite it the "class" syntax.
7+
The `Clock` class مكتوب بأسلوب وظيفي. أعد كتابتها بصيغة "class".
88

9-
P.S. The clock ticks in the console, open it to see.
9+
ملاحظة. تدق الساعة في وحدة التحكم ، افتحها لترى.

1-js/09-classes/01-class/article.md

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11

2-
# Class basic syntax
2+
# الصيغة الأساسية للClass
33

44
```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+
في البرمجة الموجهة للكائنات ، * الفئة * عبارة عن قالب رمز برنامج قابل للتوسيع لإنشاء الكائنات ، ويوفر القيم الأولية للحالة (متغيرات الأعضاء) وتنفيذ السلوك (وظائف الأعضاء أو الأساليب).
66
```
77

8-
In practice, we often need to create many objects of the same kind, like users, or goods or whatever.
8+
من الناحية العملية ، نحتاج غالبًا إلى إنشاء العديد من العناصر من نفس النوع ، مثل المستخدمين أو السلع أو أيا كان.
99

10-
As we already know from the chapter <info:constructor-new>, `new function` can help with that.
10+
كما نعلم بالفعل من الفصل <info: buildor-new> ، يمكن أن تساعد `الوظيفة الجديدة` في ذلك.
1111

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+
ولكن في جافا سكريبت الحديثة ، هناك بنية "صنف" أكثر تقدمًا ، تقدم ميزات جديدة رائعة مفيدة للبرمجة الموجهة للكائنات.
1313

14-
## The "class" syntax
14+
# الصيغة الأساسية للClass
1515

16-
The basic syntax is:
16+
الصيغة الأساسية هي:
1717
```js
1818
class MyClass {
1919
// class methods
@@ -25,11 +25,11 @@ class MyClass {
2525
}
2626
```
2727

28-
Then use `new MyClass()` to create a new object with all the listed methods.
28+
ثم استخدم `New MyClass ()` لإنشاء كائن جديد بكل الطرق المدرجة.
2929

30-
The `constructor()` method is called automatically by `new`, so we can initialize the object there.
30+
يتم استدعاء طريقة `constructor ()` تلقائيًا بواسطة `new` ، حتى نتمكن من تهيئة الكائن هناك.
3131

32-
For example:
32+
فمثلا:
3333

3434
```js run
3535
class User {
@@ -49,28 +49,28 @@ let user = new User("John");
4949
user.sayHi();
5050
```
5151

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. يعمل "المُنشئ" مع الوسيطة المحددة ويعين "اسم هذا" إليه.
5555

56-
...Then we can call object methods, such as `user.sayHi()`.
56+
... ثم يمكننا استدعاء طرق الكائن ، مثل `user.sayHi ()`.
5757

5858

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+
من المشاكل الشائعة للمطورين المبتدئين وضع فاصلة بين طرق الفصل ، مما يؤدي إلى خطأ في بناء الجملة.
6161

62-
The notation here is not to be confused with object literals. Within the class, no commas are required.
63-
```
62+
لا يجب الخلط بين التدوين هنا وحرف الشيء. داخل الفصل الدراسي ، لا يلزم وجود فواصل.
63+
``
6464

65-
## What is a class?
65+
## ما هي الclass ؟
6666

67-
So, what exactly is a `class`? That's not an entirely new language-level entity, as one might think.
67+
إذن ، ما هي "الطبقة" بالضبط؟ هذا ليس كيانًا جديدًا تمامًا على مستوى اللغة ، كما قد يعتقد المرء.
6868

69-
Let's unveil any magic and see what a class really is. That'll help in understanding many complex aspects.
69+
دعونا نكشف عن أي سحر ونرى ما هو الصف حقا. سيساعد ذلك في فهم العديد من الجوانب المعقدة.
7070

71-
In JavaScript, a class is a kind of function.
71+
في JavaScript ، الفئة هي نوع من الوظائف.
7272

73-
Here, take a look:
73+
هنا ، ألق نظرة:
7474

7575
```js run
7676
class User {
@@ -84,18 +84,18 @@ alert(typeof User); // function
8484
*/!*
8585
```
8686

87-
What `class User {...}` construct really does is:
87+
ما يفعله بناء `مستخدم الفئة {... }` حقًا هو:
8888

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

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>. لذا فإن الكائن لديه حق الوصول إلى أساليب الفصل.
9393

94-
We can illustrate the result of `class User` declaration as:
94+
يمكننا توضيح نتيجة إعلان "مستخدم الفئة" كما يلي:
9595

9696
![](class-user.svg)
9797

98-
Here's the code to introspect it:
98+
إليك كود لتتفكر فيه:
9999

100100
```js run
101101
class User {
@@ -116,9 +116,9 @@ alert(User.prototype.sayHi); // alert(this.name);
116116
alert(Object.getOwnPropertyNames(User.prototype)); // constructor, sayHi
117117
```
118118

119-
## Not just a syntactic sugar
119+
## ليس مجرد سكر نحوي
120120

121-
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" على الإطلاق:
122122

123123
```js run
124124
// rewriting class User in pure functions
@@ -140,13 +140,13 @@ let user = new User("John");
140140
user.sayHi();
141141
```
142142

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" سكرًا نحويًا لتحديد المنشئ مع طرق النموذج الأولي.
144144

145-
Still, there are important differences.
145+
لا تزال هناك اختلافات مهمة.
146146

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 "`. لذلك فهي ليست تمامًا مثل إنشائها يدويًا.
148148

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

151151
```js run
152152
class User {
@@ -157,7 +157,7 @@ Still, there are important differences.
157157
User(); // Error: Class constructor User cannot be invoked without 'new'
158158
```
159159

160-
Also, a string representation of a class constructor in most JavaScript engines starts with the "class..."
160+
أيضًا ، يبدأ تمثيل السلسلة لمنشئ فئة في معظم محركات JavaScript بـ "class ..."
161161

162162
```js run
163163
class User {
@@ -166,24 +166,23 @@ Still, there are important differences.
166166

167167
alert(User); // class User { ... }
168168
```
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+
هناك اختلافات أخرى ، سنراها قريبًا.
173170

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

176-
3. Classes always `use strict`.
177-
All code inside the class construct is automatically in strict mode.
174+
     هذا أمر جيد ، لأنه إذا كنا 'for..in` فوق كائن ما ، فإننا عادة لا نريد طرقه الطبقية.
178175

179-
Besides, `class` syntax brings many other features that we'll explore later.
176+
3. الclasses دائمًا "استخدام صارم".
177+
     تكون جميع التعليمات البرمجية داخل بنية الفصل تلقائيًا في وضع صارم.
180178

181-
## Class Expression
179+
بالإضافة إلى ذلك ، فإن بناء جملة `class` يجلب العديد من الميزات الأخرى التي سنستكشفها لاحقًا.
182180

183-
Just like functions, classes can be defined inside another expression, passed around, returned, assigned, etc.
181+
## تعبير الclass
184182

185-
Here's an example of a class expression:
183+
تمامًا مثل functions ، يمكن تعريف الفئات داخل تعبير آخر ، وتمريرها ، وإعادتها ، وتعيينها ، وما إلى ذلك.
186184

185+
إليك مثالاً على تعبير class:
187186
```js
188187
let User = class {
189188
sayHi() {
@@ -192,9 +191,9 @@ let User = class {
192191
};
193192
```
194193

195-
Similar to Named Function Expressions, class expressions may have a name.
194+
على غرار تعبيرات الوظائف المسماة ، قد يكون لتعبيرات الفئة اسم.
196195

197-
If a class expression has a name, it's visible inside the class only:
196+
إذا كان تعبير فئة له اسم ، فإنه يكون مرئيًا داخل الفصل فقط:
198197

199198
```js run
200199
// "Named Class Expression"
@@ -210,7 +209,7 @@ new User().sayHi(); // works, shows MyClass definition
210209
alert(MyClass); // error, MyClass name isn't visible outside of the class
211210
```
212211

213-
We can even make classes dynamically "on-demand", like this:
212+
يمكننا أيضًا أن نجعل classes ديناميكيًا "حسب الطلب" ، مثل هذا:
214213

215214
```js run
216215
function makeClass(phrase) {
@@ -231,9 +230,9 @@ new User().sayHi(); // Hello
231230

232231
## Getters/setters
233232

234-
Just like literal objects, classes may include getters/setters, computed properties etc.
233+
تمامًا مثل الأشياء الحرفية ، قد تتضمن الفئات الحروف / المستوطنين والخصائص المحسوبة وما إلى ذلك.
235234

236-
Here's an example for `user.name` implemented using `get/set`:
235+
في ما يلي مثال لـ `user.name` تم تنفيذه باستخدام` get / set`:
237236

238237
```js run
239238
class User {
@@ -267,11 +266,11 @@ alert(user.name); // John
267266
user = new User(""); // Name is too short.
268267
```
269268

270-
Technically, such class declaration works by creating getters and setters in `User.prototype`.
269+
من الناحية الفنية ، يعمل إعلان الفئة هذا عن طريق إنشاء الحروف والمستوطنين في `User.prototype`.
271270

272-
## Computed names [...]
271+
## الأسماء المحسوبة [...]
273272

274-
Here's an example with a computed method name using brackets `[...]`:
273+
في ما يلي مثال باسم الطريقة المحسوبة باستخدام الأقواس `[...]`:
275274

276275
```js run
277276
class User {
@@ -287,19 +286,19 @@ class User {
287286
new User().sayHi();
288287
```
289288

290-
Such features are easy to remember, as they resemble that of literal objects.
289+
من السهل تذكر هذه الميزات ، لأنها تشبه تلك الموجودة في الأشياء الحرفية.
291290

292-
## Class fields
291+
## حقول class
293292

294-
```warn header="Old browsers may need a polyfill"
295-
Class fields are a recent addition to the language.
296-
```
293+
"` `warn header =" قد تحتاج المتصفحات القديمة إلى ملف متعدد "
294+
تعد حقول الصف إضافة حديثة للغة.
295+
``
297296

298-
Previously, our classes only had methods.
297+
في السابق ، كانت فصولنا تمتلك طرقًا فقط.
299298

300-
"Class fields" is a syntax that allows to add any properties.
299+
"حقول class" هي بنية تسمح بإضافة أي خصائص.
301300

302-
For instance, let's add `name` property to `class User`:
301+
على سبيل المثال ، دعنا نضيف خاصية `name` إلى` class User`:
303302

304303
```js run
305304
class User {
@@ -315,9 +314,9 @@ class User {
315314
new User().sayHi(); // Hello, John!
316315
```
317316

318-
So, we just write "<property name> = <value>" in the declaration, and that's it.
317+
لذلك ، نكتب فقط "<اسم الخاصية> = <قيمة>" في الإعلان ، وهذا كل شيء.
319318

320-
The important difference of class fields is that they are set on individual objects, not `User.prototype`:
319+
الاختلاف المهم في حقول الصف هو أنه يتم تعيينها على كائنات فردية ، وليس `User.prototype`:
321320

322321
```js run
323322
class User {
@@ -331,7 +330,7 @@ alert(user.name); // John
331330
alert(User.prototype.name); // undefined
332331
```
333332

334-
Technically, they are processed after the constructor has done it's job, and we can use for them complex expressions and function calls:
333+
من الناحية الفنية ، تتم معالجتها بعد أن يقوم المنشئ بعمله ، ويمكننا استخدامه بالنسبة لهم التعبيرات المعقدة واستدعاءات الوظائف:
335334

336335
```js run
337336
class User {
@@ -344,13 +343,14 @@ let user = new User();
344343
alert(user.name); // John
345344
```
346345

347-
### Making bound methods with class fields
346+
### عمل طرق مرتبطة بحقول class
347+
348+
كما هو موضح في الفصل <info: bind> ، فإن وظائف JavaScript لها ديناميكية `this`. يعتمد ذلك على سياق المكالمة.
348349

349-
As demonstrated in the chapter <info:bind> functions in JavaScript have a dynamic `this`. It depends on the context of the call.
350+
لذلك إذا تم تمرير طريقة كائن واستدعاؤها في سياق آخر ، فلن يكون `هذا` مرجعاً إلى كائنه بعد الآن.
350351

351-
So if an object method is passed around and called in another context, `this` won't be a reference to its object any more.
352+
على سبيل المثال ، سيُظهر هذا الرمز `undefined`:
352353

353-
For instance, this code will show `undefined`:
354354

355355
```js run
356356
class Button {
@@ -370,12 +370,12 @@ setTimeout(button.click, 1000); // undefined
370370
*/!*
371371
```
372372

373-
The problem is called "losing `this`".
373+
تسمى المشكلة "فقدان" this "".
374374

375-
There are two approaches to fixing it, as discussed in the chapter <info:bind>:
375+
هناك طريقتان لإصلاحها ، كما هو موضح في الفصل <info: bind>:
376376

377-
1. Pass a wrapper-function, such as `setTimeout(() => button.click(), 1000)`.
378-
2. Bind the method to object, e.g. in the constructor:
377+
1. قم بتمرير دالة مجمعة ، مثل `setTimeout (() => button.click ()، 1000)`.
378+
2. ربط طريقة الاعتراض ، على سبيل المثال في المنشئ:
379379

380380
```js run
381381
class Button {
@@ -398,7 +398,7 @@ setTimeout(button.click, 1000); // hello
398398
*/!*
399399
```
400400

401-
Class fields provide a more elegant syntax for the latter solution:
401+
توفر حقول class بنية أكثر أناقة للحل الأخير:
402402

403403
```js run
404404
class Button {
@@ -417,13 +417,13 @@ let button = new Button("hello");
417417
setTimeout(button.click, 1000); // hello
418418
```
419419

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` الصحيح.
421421

422-
That's especially useful in browser environment, when we need to setup a method as an event listener.
422+
هذا مفيد بشكل خاص في بيئة المتصفح ، عندما نحتاج إلى إعداد طريقة كمستمع للأحداث.
423423

424-
## Summary
424+
## ملخص
425425

426-
The basic class syntax looks like this:
426+
تبدو بنية الصف الأساسية كما يلي:
427427

428428
```js
429429
class MyClass {
@@ -443,6 +443,6 @@ class MyClass {
443443
}
444444
```
445445

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

448-
In the next chapters we'll learn more about classes, including inheritance and other features.
448+
في الفصول التالية سنتعلم المزيد عن الفصول ، بما في ذلك الميراث والميزات الأخرى.

0 commit comments

Comments
 (0)