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
لماذا راجعنا هذه الأمثلة؟ هل يجب أن نتذكر هذه الخصائص المميزة طوال الوقت؟ حسنًا ، ليس حقًا. في الواقع ، ستصبح هذه الأشياء الصعبة مألوفة تدريجيًا بمرور الوقت ، ولكن هناك طريقة صلبة للتهرب من المشاكل معها:
203
205
204
-
فقط تعامل مع أي مقارنة بـ "undefined / null" باستثناء المساواة الصارمة `===` مع رعاية استثنائية.
205
-
206
-
لا تستخدم المقارنات `> => <<=` مع متغير قد يكون `فارغًا / غير محدد` ، ما لم تكن متأكدًا حقًا مما تفعله. إذا كان المتغير يمكن أن يكون له هذه القيم ، تحقق منها بشكل منفصل.
206
+
- فقط تعامل مع أي مقارنة بـ "undefined / null" باستثناء المساواة الصارمة `===` مع رعاية استثنائية.
207
+
- لا تستخدم المقارنات `> => <<=` مع متغير قد يكون `فارغًا / غير محدد` ، ما لم تكن متأكدًا حقًا مما تفعله. إذا كان المتغير يمكن أن يكون له هذه القيم ، تحقق منها بشكل منفصل.
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
+
classAnimal {
303
+
name ='animal'
304
+
305
+
constructor() {
306
+
alert(this.name); // (*)
307
+
}
308
+
}
309
+
310
+
classRabbitextendsAnimal {
311
+
name ='rabbit';
312
+
}
313
+
314
+
newAnimal(); // animal
315
+
*!*
316
+
newRabbit(); // 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
+
classAnimal {
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
+
classRabbitextendsAnimal {
346
+
showName() {
347
+
alert('rabbit');
348
+
}
349
+
}
350
+
351
+
newAnimal(); // animal
352
+
*!*
353
+
newRabbit(); // 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.
283
378
284
379
## Super: الأجزاء الداخلية ، [[HomeObject]]
285
380
@@ -532,4 +627,4 @@ rabbit.eat(); // Error calling super (because there's no [[HomeObject]])
532
627
- لذا ليس من الآمن نسخ طريقة باستخدام "super" من كائن إلى آخر.
533
628
534
629
أيضا:
535
-
- لا تحتوي وظائف السهم على "هذا" أو "فائق" خاص بها ، لذا فهي تتناسب بشفافية مع السياق المحيط.
630
+
- لا تحتوي وظائف السهم على "هذا" أو "فائق" خاص بها ، لذا فهي تتناسب بشفافية مع السياق المحيط.
Copy file name to clipboardExpand all lines: 1-js/13-modules/01-modules-intro/article.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,3 @@
1
-
2
1
# مقدّمة إلى الوِحدات
3
2
4
3
سنرى سريعًا بينما تطبيقنا يكبُر حجمًا وتعقيدًا بأنّ علينا تقسيمه إلى ملفات متعدّدة، أو ”وِحدات“ (module). عادةً ما تحتوي الوِحدة على صنف أو مكتبة فيها دوالّ.
@@ -57,6 +56,9 @@ sayHi('John'); // Hello, John!
57
56
58
57
يجلب المتصفّح الوِحدة تلقائيًا ويقيم الشيفرة البرمجية بداخلها (ويستورد جميع الوحدات المتعلقة بها إن لزم الأمر)، وثمّ يشغلها.
59
58
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.
Copy file name to clipboardExpand all lines: 2-ui/1-document/01-browser-environment/article.md
+5-7Lines changed: 5 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
3
3
The JavaScript language was initially created for web browsers. Since then it has evolved and become a language with many uses and platforms.
4
4
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*.
6
6
7
7
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.
8
8
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 webbrowser:
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).
55
53
56
54
```smart header="DOM is not only for browsers"
57
55
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
60
58
```
61
59
62
60
```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.
64
62
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.
Copy file name to clipboardExpand all lines: 2-ui/1-document/07-modifying-document/article.md
+14-33Lines changed: 14 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,54 +48,35 @@ DOM التعديل هو مفتاح إنشاء صفحات "حية".
48
48
let textNode = document.createTextNode('Here I am');
49
49
```
50
50
51
+
في حالتنا، تكون الرسالة `div` مع فئة` تنبيه 'و HTML فيها:
52
+
51
53
### إنشاء الرسالة
52
54
53
-
في حالتنا ، تكون الرسالة `div`مع فئة` تنبيه 'و HTML فيها:
55
+
في حالتنا، إنشاء `div`الرسالة ينطلب 3 مراحل:
54
56
55
57
```js
58
+
// 1. Create <div> element
56
59
let div =document.createElement('div');
60
+
61
+
// 2. Set its class to "alert"
57
62
div.className="alert";
63
+
64
+
// Fill it with the content
58
65
div.innerHTML="<strong>Hi there!</strong> You've read an important message.";
59
66
```
60
67
61
-
لقد أنشأنا العنصر ، ولكن حتى الآن إنه متغير فقط. لا يمكننا رؤية العنصر على الصفحة ، لأنه ليس جزءًا من المستند حتى الآن.
68
+
لقد أنشأنا العنصر ، ولكن حتى الآن إنه متغير فقط. لا يمكننا رؤية العنصر على الصفحة ، لأنه ليس جزءًا من المستند حتى الآن
62
69
63
70
## طرق الإدراج
64
71
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: 1pxsolid#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) `.
92
73
93
74
توفر هذه المجموعة من الطرق المزيد من الطرق لإدراج:
94
75
95
76
-`node.append (... nodes or strings)` - إلحاق عقد أو سلاسل في نهاية `node` ،
96
77
-`node.prepend (... العقد أو السلاسل)` - إدراج العقد أو السلاسل في بداية `العقدة` ،
97
78
-`node.before (... nodes or strings)` –- أدخل العقد أو السلاسل قبل `node` ،
98
-
-`العقدة بعد (... العقد أو السلاسل)` - - إدراج العقد أو السلاسل بعد `العقدة` ،
79
+
-`node.after(...nodes or strings)` - إدراج العقد أو السلاسل بعد `العقدة` ،
99
80
-`node.replaceWith (... العقد أو السلاسل)` - - يستبدل `العقدة` بالعقد أو السلاسل المعطاة.
100
81
101
82
فيما يلي مثال على استخدام هذه الأساليب لإضافة عناصر إلى قائمة والنص قبلها / بعدها:
@@ -150,7 +131,7 @@ after
150
131
</script>
151
132
```
152
133
153
-
يتم إدراج كل النص * كنص *.
134
+
يتم إدراج كل النص * كنص *.
154
135
155
136
إذن HTML النهائي هو:
156
137
@@ -166,7 +147,7 @@ after
166
147
167
148
لذلك ، لا يمكن استخدام هذه الطرق إلا لإدراج عقد DOM أو أجزاء نصية.
168
149
169
-
ولكن ماذا لو أردنا إدراج HTML "كـ html" ، مع عمل جميع العلامات والأشياء ، مثل `elem.innerHTML`؟
150
+
ولكن ماذا لو أردنا إدراج HTML "كـ html" ، مع عمل جميع العلامات والأشياء ، مثل `elem.innerHTML`?
Copy file name to clipboardExpand all lines: 9-regular-expressions/06-regexp-boundary/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ So, it matches the pattern `pattern:\bHello\b`, because:
25
25
26
26
1. At the beginning of the string matches the first test `pattern:\b`.
27
27
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.
29
29
30
30
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).
0 commit comments