Skip to content

Commit 523808a

Browse files
authored
Merge pull request #124 from javascript-tutorial/sync-445bda39
Sync with upstream @ 445bda3
2 parents 58381bc + c54a876 commit 523808a

8 files changed

Lines changed: 125 additions & 71 deletions

File tree

1-js/02-first-steps/09-comparison/article.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
- يساوي: `a == b` ، يرجى ملاحظة أن علامة المساواة المزدوجة` = `تعني اختبار المساواة ، في حين أن كلمة واحدة` a = b` تعني تعيين أو مساواة .
1010
- لا تساوي. في الرياضيات يكون الترميز <code>&ne;</code> ،لكن في JavaScript تكتب هكذا <code>a != b</code>.
1111

12-
في هذه المقالة سنتعلم المزيد عن الأنواع المختلفة من المقارنات ، وكيف تجعلها JavaScript ، بما في ذلك الخصائص المهمة.
12+
في هذه المقالة سنتعلم المزيد عن الأنواع المختلفة من المقارنات ، وكيف تجعلها JavaScript، بما في ذلك الخصائص المهمة.
13+
14+
في النهاية ستجد وصفة جيدة لتجنب المشاكل المتعلقة بـ "مراوغات JavaScript".
1315

1416
## Boolean هي النتيجة
1517

@@ -201,9 +203,8 @@ alert( undefined == 0 ); // false (3)
201203

202204
لماذا راجعنا هذه الأمثلة؟ هل يجب أن نتذكر هذه الخصائص المميزة طوال الوقت؟ حسنًا ، ليس حقًا. في الواقع ، ستصبح هذه الأشياء الصعبة مألوفة تدريجيًا بمرور الوقت ، ولكن هناك طريقة صلبة للتهرب من المشاكل معها:
203205

204-
فقط تعامل مع أي مقارنة بـ "undefined / null" باستثناء المساواة الصارمة `===` مع رعاية استثنائية.
205-
206-
لا تستخدم المقارنات `> => <<=` مع متغير قد يكون `فارغًا / غير محدد` ، ما لم تكن متأكدًا حقًا مما تفعله. إذا كان المتغير يمكن أن يكون له هذه القيم ، تحقق منها بشكل منفصل.
206+
- فقط تعامل مع أي مقارنة بـ "undefined / null" باستثناء المساواة الصارمة `===` مع رعاية استثنائية.
207+
- لا تستخدم المقارنات `> => <<=` مع متغير قد يكون `فارغًا / غير محدد` ، ما لم تكن متأكدًا حقًا مما تفعله. إذا كان المتغير يمكن أن يكون له هذه القيم ، تحقق منها بشكل منفصل.
207208

208209
## ملخص
209210

1-js/05-data-types/02-number/article.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ alert( 123456..toString(36) ); // 2n9c
121121
| -1.1 | -2 | -1 | -1 | -1 |
122122
| -1.6 | -2 | -1 | -2 | -1 |
123123

124-
125-
126124
تُعَطِّي هذه التوابع جميع الاحتمالات الممكنة للتعامل مع الجزء العشري للعدد، لكن ماذا إن كنا نريد تقريب العدد إلى خانة محدَّدة بعد الفاصلة العشرية؟
127125

128126
مثلًا، لدينا العدد `1.2345` ونريد تقريب إلى خانتين لنحصل على `1.23` فقط. يوجد طريقتين للقيام بذلك:

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -377,27 +377,6 @@ setTimeout(button.click, 1000); // undefined
377377
1. قم بتمرير دالة مجمعة ، مثل `setTimeout (() => button.click ()، 1000)`.
378378
2. ربط طريقة الاعتراض ، على سبيل المثال في المنشئ:
379379

380-
```js run
381-
class Button {
382-
constructor(value) {
383-
this.value = value;
384-
*!*
385-
this.click = this.click.bind(this);
386-
*/!*
387-
}
388-
389-
click() {
390-
alert(this.value);
391-
}
392-
}
393-
394-
let button = new Button("hello");
395-
396-
*!*
397-
setTimeout(button.click, 1000); // hello
398-
*/!*
399-
```
400-
401380
توفر حقول class بنية أكثر أناقة للحل الأخير:
402381

403382
```js run

1-js/09-classes/02-class-inheritance/article.md

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ let rabbit = new Rabbit("White Rabbit", 10); // Error: this is not defined.
232232

233233
عفوًا! لدينا خطأ. الآن لا يمكننا إنشاء الأرانب. ماذا حصل؟
234234

235-
الإجابة المختصرة هي: يجب على منشئو الفصول الموروثة استدعاء `super (...)` و (!) قبل ذلك باستخدام `this`.
235+
الاجابة السريعة:
236+
237+
- **الإجابة المختصرة هي: يجب على منشئو الفصول الموروثة استدعاء `super (...)` و (!) قبل ذلك باستخدام `this`.**
236238

237239
...لكن لماذا؟ ماذا يجري هنا؟ في الواقع ، يبدو الشرط غريبًا.
238240

@@ -280,6 +282,99 @@ alert(rabbit.earLength); // 10
280282
*/!*
281283
```
282284

285+
### Overriding class fields: a tricky note
286+
287+
```warn header="Advanced note"
288+
This note assumes you have a certain experience with classes, maybe in other programming languages.
289+
290+
It provides better insight into the language and also explains the behavior that might be a source of bugs (but not very often).
291+
292+
If you find it difficult to understand, just go on, continue reading, then return to it some time later.
293+
```
294+
295+
We can override not only methods, but also class fields.
296+
297+
Although, there's a tricky behavior when we access an overridden field in parent constructor, quite different from most other programming languages.
298+
299+
Consider this example:
300+
301+
```js run
302+
class Animal {
303+
name = 'animal'
304+
305+
constructor() {
306+
alert(this.name); // (*)
307+
}
308+
}
309+
310+
class Rabbit extends Animal {
311+
name = 'rabbit';
312+
}
313+
314+
new Animal(); // animal
315+
*!*
316+
new Rabbit(); // animal
317+
*/!*
318+
```
319+
320+
Here, class `Rabbit` extends `Animal` and overrides `name` field with its own value.
321+
322+
There's no own constructor in `Rabbit`, so `Animal` constructor is called.
323+
324+
What's interesting is that in both cases: `new Animal()` and `new Rabbit()`, the `alert` in the line `(*)` shows `animal`.
325+
326+
**In other words, parent constructor always uses its own field value, not the overridden one.**
327+
328+
What's odd about it?
329+
330+
If it's not clear yet, please compare with methods.
331+
332+
Here's the same code, but instead of `this.name` field we call `this.showName()` method:
333+
334+
```js run
335+
class Animal {
336+
showName() { // instead of this.name = 'animal'
337+
alert('animal');
338+
}
339+
340+
constructor() {
341+
this.showName(); // instead of alert(this.name);
342+
}
343+
}
344+
345+
class Rabbit extends Animal {
346+
showName() {
347+
alert('rabbit');
348+
}
349+
}
350+
351+
new Animal(); // animal
352+
*!*
353+
new Rabbit(); // rabbit
354+
*/!*
355+
```
356+
357+
Please note: now the output is different.
358+
359+
And that's what we naturally expect. When the parent constructor is called in the derived class, it uses the overridden method.
360+
361+
...But for class fields it's not so. As said, the parent constructor always uses the parent field.
362+
363+
Why is there the difference?
364+
365+
Well, the reason is in the field initialization order. The class field is initialized:
366+
- Before constructor for the base class (that doesn't extend anything),
367+
- Imediately after `super()` for the derived class.
368+
369+
In our case, `Rabbit` is the derived class. There's no `constructor()` in it. As said previously, that's the same as if there was an empty constructor with only `super(...args)`.
370+
371+
So, `new Rabbit()` calls `super()`, thus executing the parent constructor, and (per the rule for derived classes) only after that its class fields are initialized. At the time of the parent constructor execution, there are no `Rabbit` class fields yet, that's why `Animal` fields are used.
372+
373+
This subtle difference between fields and methods is specific to JavaScript
374+
375+
Luckily, this behavior only reveals itself if an overridden field is used in the parent constructor. Then it may be difficult to understand what's going on, so we're explaining it here.
376+
377+
If it becomes a problem, one can fix it by using methods or getters/setters instead of fields.
283378

284379
## Super: الأجزاء الداخلية ، [[HomeObject]]
285380

@@ -532,4 +627,4 @@ rabbit.eat(); // Error calling super (because there's no [[HomeObject]])
532627
     - لذا ليس من الآمن نسخ طريقة باستخدام "super" من كائن إلى آخر.
533628

534629
أيضا:
535-
- لا تحتوي وظائف السهم على "هذا" أو "فائق" خاص بها ، لذا فهي تتناسب بشفافية مع السياق المحيط.
630+
- لا تحتوي وظائف السهم على "هذا" أو "فائق" خاص بها ، لذا فهي تتناسب بشفافية مع السياق المحيط.

1-js/13-modules/01-modules-intro/article.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# مقدّمة إلى الوِحدات
32

43
سنرى سريعًا بينما تطبيقنا يكبُر حجمًا وتعقيدًا بأنّ علينا تقسيمه إلى ملفات متعدّدة، أو ”وِحدات“ (module). عادةً ما تحتوي الوِحدة على صنف أو مكتبة فيها دوالّ.
@@ -57,6 +56,9 @@ sayHi('John'); // Hello, John!
5756

5857
يجلب المتصفّح الوِحدة تلقائيًا ويقيم الشيفرة البرمجية بداخلها (ويستورد جميع الوحدات المتعلقة بها إن لزم الأمر)، وثمّ يشغلها.
5958

59+
```warn header="Modules work only via HTTP(s), not in local files"
60+
If you try to open a web-page locally, via `file://` protocol, you'll find that `import/export` directives don't work. Use a local web-server, such as [static-server](https://www.npmjs.com/package/static-server#getting-started) or use the "live server" capability of your editor, such as VS Code [Live Server Extension](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer) to test modules.
61+
```
6062

6163
## ميزات الوِحدات الأساسية
6264
=======

2-ui/1-document/01-browser-environment/article.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
The JavaScript language was initially created for web browsers. Since then it has evolved and become a language with many uses and platforms.
44

5-
A platform may be a browser, or a web-server or another *host*, even a coffee machine. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*.
5+
A platform may be a browser, or a web-server or another *host*, even a "smart" coffee machine, if it can run JavaScript. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*.
66

77
A host environment provides own objects and functions additional to the language core. Web browsers give a means to control web pages. Node.js provides server-side features, and so on.
88

9-
Here's a bird's-eye view of what we have when JavaScript runs in a web-browser:
9+
Here's a bird's-eye view of what we have when JavaScript runs in a web browser:
1010

1111
![](windowObjects.svg)
1212

@@ -49,9 +49,7 @@ document.body.style.background = "red";
4949
setTimeout(() => document.body.style.background = "", 1000);
5050
```
5151

52-
Here we used `document.body.style`, but there's much, much more. Properties and methods are described in the specification:
53-
54-
- **DOM Living Standard** at <https://dom.spec.whatwg.org>
52+
Here we used `document.body.style`, but there's much, much more. Properties and methods are described in the specification: [DOM Living Standard](https://dom.spec.whatwg.org).
5553

5654
```smart header="DOM is not only for browsers"
5755
The DOM specification explains the structure of a document and provides objects to manipulate it. There are non-browser instruments that use DOM too.
@@ -60,9 +58,9 @@ For instance, server-side scripts that download HTML pages and process them can
6058
```
6159

6260
```smart header="CSSOM for styling"
63-
CSS rules and stylesheets are structured in a different way than HTML. There's a separate specification, [CSS Object Model (CSSOM)](https://www.w3.org/TR/cssom-1/), that explains how they are represented as objects, and how to read and write them.
61+
There's also a separate specification, [CSS Object Model (CSSOM)](https://www.w3.org/TR/cssom-1/) for CSS rules and stylesheets, that explains how they are represented as objects, and how to read and write them.
6462
65-
CSSOM is used together with DOM when we modify style rules for the document. In practice though, CSSOM is rarely required, because usually CSS rules are static. We rarely need to add/remove CSS rules from JavaScript, but that's also possible.
63+
CSSOM is used together with DOM when we modify style rules for the document. In practice though, CSSOM is rarely required, because we rarely need to modify CSS rules from JavaScript (usually we just add/remove CSS classes, not modify their CSS rules), but that's also possible.
6664
```
6765

6866
## BOM (Browser Object Model)

2-ui/1-document/07-modifying-document/article.md

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,54 +48,35 @@ DOM التعديل هو مفتاح إنشاء صفحات "حية".
4848
let textNode = document.createTextNode('Here I am');
4949
```
5050

51+
في حالتنا، تكون الرسالة `div` مع فئة` تنبيه 'و HTML فيها:
52+
5153
### إنشاء الرسالة
5254

53-
في حالتنا ، تكون الرسالة `div` مع فئة` تنبيه 'و HTML فيها:
55+
في حالتنا، إنشاء `div` الرسالة ينطلب 3 مراحل:
5456

5557
```js
58+
// 1. Create <div> element
5659
let div = document.createElement('div');
60+
61+
// 2. Set its class to "alert"
5762
div.className = "alert";
63+
64+
// Fill it with the content
5865
div.innerHTML = "<strong>Hi there!</strong> You've read an important message.";
5966
```
6067

61-
لقد أنشأنا العنصر ، ولكن حتى الآن إنه متغير فقط. لا يمكننا رؤية العنصر على الصفحة ، لأنه ليس جزءًا من المستند حتى الآن.
68+
لقد أنشأنا العنصر ، ولكن حتى الآن إنه متغير فقط. لا يمكننا رؤية العنصر على الصفحة ، لأنه ليس جزءًا من المستند حتى الآن
6269

6370
## طرق الإدراج
6471

65-
لعرض "div" ، نحتاج إلى إدراجه في مكان ما في "المستند". على سبيل المثال ، في `document.body`.
66-
67-
هناك طريقة خاصة `append` لذلك:` document.body.append (div) `.
68-
69-
إليك الكود الكامل:
70-
71-
```html run height="80"
72-
<style>
73-
.alert {
74-
padding: 15px;
75-
border: 1px solid #d6e9c6;
76-
border-radius: 4px;
77-
color: #3c763d;
78-
background-color: #dff0d8;
79-
}
80-
</style>
81-
82-
<script>
83-
let div = document.createElement('div');
84-
div.className = "alert";
85-
div.innerHTML = "<strong>Hi there!</strong> You've read an important message.";
86-
87-
*!*
88-
document.body.append(div);
89-
*/!*
90-
</script>
91-
```
72+
لعرض "div" ، نحتاج إلى إدراجه في مكان ما في "المستند". على سبيل المثال ، في `document.body`. هناك طريقة خاصة `append` لذلك:` document.body.append (div) `.
9273

9374
توفر هذه المجموعة من الطرق المزيد من الطرق لإدراج:
9475

9576
- `node.append (... nodes or strings)` - إلحاق عقد أو سلاسل في نهاية `node` ،
9677
- `node.prepend (... العقد أو السلاسل)` - إدراج العقد أو السلاسل في بداية `العقدة` ،
9778
- `node.before (... nodes or strings)` –- أدخل العقد أو السلاسل قبل `node` ،
98-
- `العقدة بعد (... العقد أو السلاسل)` - - إدراج العقد أو السلاسل بعد `العقدة` ،
79+
- `node.after(...nodes or strings)` - إدراج العقد أو السلاسل بعد `العقدة` ،
9980
- `node.replaceWith (... العقد أو السلاسل)` - - يستبدل `العقدة` بالعقد أو السلاسل المعطاة.
10081

10182
فيما يلي مثال على استخدام هذه الأساليب لإضافة عناصر إلى قائمة والنص قبلها / بعدها:
@@ -150,7 +131,7 @@ after
150131
</script>
151132
```
152133

153-
يتم إدراج كل النص * كنص *.
134+
يتم إدراج كل النص * كنص *.
154135

155136
إذن HTML النهائي هو:
156137

@@ -166,7 +147,7 @@ after
166147

167148
لذلك ، لا يمكن استخدام هذه الطرق إلا لإدراج عقد DOM أو أجزاء نصية.
168149

169-
ولكن ماذا لو أردنا إدراج HTML "كـ html" ، مع عمل جميع العلامات والأشياء ، مثل `elem.innerHTML`؟
150+
ولكن ماذا لو أردنا إدراج HTML "كـ html" ، مع عمل جميع العلامات والأشياء ، مثل `elem.innerHTML`?
170151

171152
## insertAdjacentHTML / Text / Element
172153

@@ -545,4 +526,4 @@ ul.append(...getListContent()); // append + "..." operator = friends!
545526
- لإلحاق HTML بالصفحة قبل أن ينتهي التحميل:
546527
- `document.write (html)`
547528

548-
بعد تحميل الصفحة تقوم هذه المكالمة بمسح المستند. غالبا ما ينظر إليها في النصوص القديمة.
529+
بعد تحميل الصفحة تقوم هذه المكالمة بمسح المستند. غالبا ما ينظر إليها في النصوص القديمة.

9-regular-expressions/06-regexp-boundary/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ So, it matches the pattern `pattern:\bHello\b`, because:
2525

2626
1. At the beginning of the string matches the first test `pattern:\b`.
2727
2. Then matches the word `pattern:Hello`.
28-
3. Then the test `pattern:\b` matches again, as we're between `subject:o` and a space.
28+
3. Then the test `pattern:\b` matches again, as we're between `subject:o` and a comma.
2929

3030
The pattern `pattern:\bHello\b` would also match. But not `pattern:\bHell\b` (because there's no word boundary after `l`) and not `Java!\b` (because the exclamation sign is not a wordly character `pattern:\w`, so there's no word boundary after it).
3131

0 commit comments

Comments
 (0)