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
- المقارنات `(1)` و `(2)` إرجاع `false` لأنه يتم تحويل` undefined` إلى `NaN` و` NaN` هي قيمة رقمية خاصة تُرجع `false` لجميع المقارنات.
198
204
- تحقق المساواة `((3)` تُرجع `false` لأن` undefined` تساوي فقط `null` و` undefined` ولا قيمة أخرى.
199
205
206
+
<<<<<<< HEAD
200
207
### تجنب المشاكل
201
208
202
209
لماذا راجعنا هذه الأمثلة؟ هل يجب أن نتذكر هذه الخصائص المميزة طوال الوقت؟ حسنًا ، ليس حقًا. في الواقع ، ستصبح هذه الأشياء الصعبة مألوفة تدريجيًا بمرور الوقت ، ولكن هناك طريقة صلبة للتهرب من المشاكل معها:
203
210
204
211
فقط تعامل مع أي مقارنة بـ "undefined / null" باستثناء المساواة الصارمة `===` مع رعاية استثنائية.
205
212
206
213
لا تستخدم المقارنات `> => <<=` مع متغير قد يكون `فارغًا / غير محدد` ، ما لم تكن متأكدًا حقًا مما تفعله. إذا كان المتغير يمكن أن يكون له هذه القيم ، تحقق منها بشكل منفصل.
214
+
=======
215
+
### Avoid problems
216
+
217
+
Why did we go over these examples? Should we remember these peculiarities all the time? Well, not really. Actually, these tricky things will gradually become familiar over time, but there's a solid way to avoid problems with them:
218
+
219
+
- Treat any comparison with `undefined/null` except the strict equality `===` with exceptional care.
220
+
- Don't use comparisons `>= > < <=` with a variable which may be `null/undefined`, unless you're really sure of what you're doing. If a variable can have these values, check for them separately.
For example, to round the number to the 2nd digit after the decimal, we can multiply the number by `100` (or a bigger power of 10), call the rounding function and then divide it back.
1. Pass a wrapper-function, such as `setTimeout(() => button.click(), 1000)`.
414
+
2. Bind the method to object, e.g. in the constructor.
415
+
416
+
Class fields provide another, quite elegant syntax:
417
+
>>>>>>> 445bda39806050acd96f87166a7c97533a0c67e9
402
418
403
419
```js run
404
420
classButton {
@@ -417,9 +433,15 @@ let button = new Button("hello");
417
433
setTimeout(button.click, 1000); // hello
418
434
```
419
435
436
+
<<<<<<< HEAD
420
437
ينشئ حقل الفئة `click = () => {...}` وظيفة مستقلة على كل كائن `Button` ، مع` this` مرتبطًا بالكائن. ثم يمكننا تمرير "button.click" في أي مكان ، وسيتم استدعاؤها باستخدام `this` الصحيح.
421
438
422
439
هذا مفيد بشكل خاص في بيئة المتصفح ، عندما نحتاج إلى إعداد طريقة كمستمع للأحداث.
440
+
=======
441
+
The class field `click = () => {...}` is created on a per-object basis, there's a separate function for each `Button` object, with `this` inside it referencing that object. We can pass `button.click` around anywhere, and the value of `this` will always be correct.
442
+
443
+
That's especially useful in browser environment, for event listeners.
Copy file name to clipboardExpand all lines: 1-js/09-classes/02-class-inheritance/article.md
+110Lines changed: 110 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -232,7 +232,13 @@ let rabbit = new Rabbit("White Rabbit", 10); // Error: this is not defined.
232
232
233
233
عفوًا! لدينا خطأ. الآن لا يمكننا إنشاء الأرانب. ماذا حصل؟
234
234
235
+
<<<<<<< HEAD
235
236
الإجابة المختصرة هي: يجب على منشئو الفصول الموروثة استدعاء `super (...)` و (!) قبل ذلك باستخدام `this`.
237
+
=======
238
+
The short answer is:
239
+
240
+
-**Constructors in inheriting classes must call `super(...)`, and (!) do it before using `this`.**
241
+
>>>>>>> 445bda39806050acd96f87166a7c97533a0c67e9
236
242
237
243
...لكن لماذا؟ ماذا يجري هنا؟ في الواقع ، يبدو الشرط غريبًا.
238
244
@@ -245,7 +251,11 @@ let rabbit = new Rabbit("White Rabbit", 10); // Error: this is not defined.
245
251
- عندما يتم تنفيذ وظيفة عادية باستخدام `new` ، فإنها تنشئ كائنًا فارغًا وتعينه بـ` this`.
246
252
- ولكن عندما يعمل منشئ مشتق ، فإنه لا يفعل ذلك. وتتوقع من المُنشئ الأصلي أن يقوم بهذه المهمة.
247
253
254
+
<<<<<<< HEAD
248
255
لذا يجب على المُنشئ المشتق استدعاء `super` من أجل تنفيذ مُنشئه الأصلي (غير المُشتق) ، وإلا فلن يتم إنشاء كائن` this`. وسنحصل على خطأ.
256
+
=======
257
+
So a derived constructor must call `super` in order to execute its parent (base) constructor, otherwise the object for `this` won't be created. And we'll get an error.
258
+
>>>>>>> 445bda39806050acd96f87166a7c97533a0c67e9
249
259
250
260
لكي يعمل مُنشئ "الأرنب" ، يجب الاتصال بـ "super ()` قبل استخدام `this` ، كما يلي:
This note assumes you have a certain experience with classes, maybe in other programming languages.
302
+
303
+
It provides better insight into the language and also explains the behavior that might be a source of bugs (but not very often).
304
+
305
+
If you find it difficult to understand, just go on, continue reading, then return to it some time later.
306
+
```
307
+
308
+
We can override not only methods, but also class fields.
309
+
310
+
Although, there's a tricky behavior when we access an overridden field in parent constructor, quite different from most other programming languages.
311
+
312
+
Consider this example:
313
+
314
+
```js run
315
+
classAnimal {
316
+
name ='animal'
317
+
318
+
constructor() {
319
+
alert(this.name); // (*)
320
+
}
321
+
}
322
+
323
+
classRabbitextendsAnimal {
324
+
name ='rabbit';
325
+
}
326
+
327
+
newAnimal(); // animal
328
+
*!*
329
+
newRabbit(); // animal
330
+
*/!*
331
+
```
332
+
333
+
Here, class `Rabbit` extends `Animal` and overrides `name` field with its own value.
334
+
335
+
There's no own constructor in `Rabbit`, so `Animal` constructor is called.
336
+
337
+
What's interesting is that in both cases: `new Animal()` and `new Rabbit()`, the `alert` in the line `(*)` shows `animal`.
338
+
339
+
**In other words, parent constructor always uses its own field value, not the overridden one.**
340
+
341
+
What's odd about it?
342
+
343
+
If it's not clear yet, please compare with methods.
344
+
345
+
Here's the same code, but instead of `this.name` field we call `this.showName()` method:
346
+
347
+
```js run
348
+
classAnimal {
349
+
showName() { // instead of this.name = 'animal'
350
+
alert('animal');
351
+
}
352
+
353
+
constructor() {
354
+
this.showName(); // instead of alert(this.name);
355
+
}
356
+
}
357
+
358
+
classRabbitextendsAnimal {
359
+
showName() {
360
+
alert('rabbit');
361
+
}
362
+
}
363
+
364
+
newAnimal(); // animal
365
+
*!*
366
+
newRabbit(); // rabbit
367
+
*/!*
368
+
```
369
+
370
+
Please note: now the output is different.
371
+
372
+
And that's what we naturally expect. When the parent constructor is called in the derived class, it uses the overridden method.
373
+
374
+
...But for class fields it's not so. As said, the parent constructor always uses the parent field.
375
+
376
+
Why is there the difference?
377
+
378
+
Well, the reason is in the field initialization order. The class field is initialized:
379
+
- Before constructor for the base class (that doesn't extend anything),
380
+
- Imediately after `super()` for the derived class.
381
+
382
+
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)`.
383
+
384
+
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.
385
+
386
+
This subtle difference between fields and methods is specific to JavaScript
387
+
388
+
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.
389
+
390
+
If it becomes a problem, one can fix it by using methods or getters/setters instead of fields.
391
+
392
+
393
+
## Super: internals, [[HomeObject]]
394
+
>>>>>>> 445bda39806050acd96f87166a7c97533a0c67e9
285
395
286
396
```warn header="معلومات متقدمة"
287
397
إذا كنت تقرأ البرنامج التعليمي لأول مرة - فقد يتم تخطي هذا القسم.
Copy file name to clipboardExpand all lines: 1-js/13-modules/01-modules-intro/article.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,41 @@
1
1
2
2
# مقدّمة إلى الوِحدات
3
3
4
+
<<<<<<< HEAD
4
5
سنرى سريعًا بينما تطبيقنا يكبُر حجمًا وتعقيدًا بأنّ علينا تقسيمه إلى ملفات متعدّدة، أو ”وِحدات“ (module). عادةً ما تحتوي الوِحدة على صنف أو مكتبة فيها دوالّ.
6
+
=======
7
+
As our application grows bigger, we want to split it into multiple files, so called "modules". A module may contain a class or a library of functions for a specific purpose.
8
+
>>>>>>> 445bda39806050acd96f87166a7c97533a0c67e9
5
9
6
10
كانت محرّكات جافاسكربت تعمل لفترة طويلة جدًا دون أيّ صياغة وِحدات على مستوى اللغة، ولم تكن هذه بالمشكلة إذ أنّ السكربتات سابقًا كانت بسيطة وسهلة ولم يكن هناك داعٍ فعلي للوِحدات.
7
11
8
12
ولكن كالعادة صارت السكربتات هذه أكثر تعقيدًا وأكبر، فكان على المجتمع اختراع طرائق مختلفة لتنظيم الشيفرات في وحدات (أو مكتبات خاصّة تُحمّل تلك الوِحدات حين الطلب).
9
13
14
+
<<<<<<< HEAD
10
15
مثال:
16
+
=======
17
+
To name some (for historical reasons):
18
+
>>>>>>> 445bda39806050acd96f87166a7c97533a0c67e9
11
19
12
20
-[AMD](https://en.wikipedia.org/wiki/Asynchronous_module_definition): هذه إحدى نُظم المكتبات القديمة جدًا والتي كتبت تنفيذها بدايةً المكتبة [require.js](http://requirejs.org/).
13
21
-[CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1): نظام الوِحدات الذي صُنِع لخوادم Node.js.
14
22
-[UMD](https://github.com/umdjs/umd): نظام وِحدات آخر (اقتُرح ليكون للعموم أجمعين) وهو متوافق مع AMD وCommonJS.
15
23
16
24
أمّا الآن فهذه المكتبات صارت (أو تصير، يومًا بعد آخر) جزءًا من التاريخ، ولكن مع ذلك سنراها في السكربتات القديمة.
17
25
26
+
<<<<<<< HEAD
18
27
ظهر نظام الوِحدات (على مستوى اللغة) في المعيار عام 2015، وتطوّر شيئًا فشيئًا منذئذ وصارت الآن أغلب المتصفّحات الرئيسة (كما و Node.js) تدعمه. لذا سيكون أفضل لو بدأنا دراسة عملها من الآن.
28
+
=======
29
+
The language-level module system appeared in the standard in 2015, gradually evolved since then, and is now supported by all major browsers and in Node.js. So we'll study the modern JavaScript modules from now on.
30
+
>>>>>>> 445bda39806050acd96f87166a7c97533a0c67e9
19
31
20
32
## ما الوِحدة؟
21
33
34
+
<<<<<<< HEAD
22
35
الوِحدة هي ملف، فقط. كلّ نص برمجي يساوي وحدة واحدة.
36
+
=======
37
+
A module is just a file. One script is one module. As simple as that.
38
+
>>>>>>> 445bda39806050acd96f87166a7c97533a0c67e9
23
39
24
40
يمكن أن تُحمّل الوِحدات بعضها البعض وتستعمل توجيهات خاصة مثل التصدير `export` والاستيراد `import` لتتبادل الميزات فيما بينها وتستدعي الدوالّ الموجودة في وحدة ص، من وحدة س:
يجلب المتصفّح الوِحدة تلقائيًا ويقيم الشيفرة البرمجية بداخلها (ويستورد جميع الوحدات المتعلقة بها إن لزم الأمر)، وثمّ يشغلها.
76
+
=======
77
+
The browser automatically fetches and evaluates the imported module (and its imports if needed), and then runs the script.
78
+
79
+
```warn header="Modules work only via HTTP(s), not in local files"
80
+
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.
0 commit comments