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/11-async/06-promisify/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Promisification -- is a long word for a simple transform. It's conversion of a function that accepts a callback into a function returning a promise.
4
4
5
-
In other words, we create a wrapper-function that does the same, internally calling the original one, but returns a promise.
5
+
To be more precise, we create a wrapper-function that does the same, internally calling the original one, but returns a promise.
6
6
7
7
Such transforms are often needed in real-life, as many functions and libraries are callback-based. But promises are more convenient. So it makes sense to promisify those.
8
8
@@ -105,7 +105,7 @@ f = promisify(f, true);
105
105
f(...).then(arrayOfResults=>..., err=>...)
106
106
```
107
107
108
-
In some cases, `err` may be absent at all: `callback(result)`, or there's something exotic in the callback format, then we can promisify such functions manually.
108
+
In some cases, `err` may be absent at all: `callback(result)`, or there's something exotic in the callback format, then we can promisify such functions without using the helper, manually.
109
109
110
110
There are also modules with a bit more flexible promisification functions, e.g. [es6-promisify](https://github.com/digitaldesignlabs/es6-promisify). In Node.js, there's a built-in `util.promisify` function for that.
Copy file name to clipboardExpand all lines: 1-js/13-modules/01-modules-intro/article.md
+20-16Lines changed: 20 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ A module usually contains a class or a library of useful functions.
6
6
7
7
For a long time, JavaScript existed without a language-level module syntax. That wasn't a problem, because initially scripts were small and simple. So there was no need.
8
8
9
-
But eventually scripts became more and more complex, so the community invented a variety of ways to organize code into modules.
9
+
But eventually scripts became more and more complex, so the community invented a variety of ways to organize code into modules, special libraries to load modules on demand.
10
10
11
11
For instance:
12
12
@@ -20,9 +20,9 @@ Now all these slowly become a part of history, but we still can find them in old
20
20
21
21
A module is just a file, a single script, as simple as that.
22
22
23
-
Directives `export` and `import`allow to interchange functionality between modules:
23
+
There are directives `export` and `import` to interchange functionality between modules, call functions of one module from another one:
24
24
25
-
-`export` keyword labels variables and functions that should be accessible from outside the file.
25
+
-`export` keyword labels variables and functions that should be accessible from outside the current module.
26
26
-`import` allows to import functionality from other modules.
27
27
28
28
For instance, if we have a file `sayHi.js` exporting a function:
@@ -44,13 +44,15 @@ alert(sayHi); // function...
44
44
sayHi('John'); // Hello, John!
45
45
```
46
46
47
-
In this tutorial we concentrate on the language itself, but we use browser as the demo environment, so let's see how modules work in the browser.
47
+
In this tutorial we concentrate on the language itself, but we use browser as the demo environment, so let's see how to use modules in the browser.
48
48
49
-
To use modules, we must set the attribute `<script type="module">`, like this:
49
+
As modules support special keywords and features, we must tell the browser that a script should be treated as module, by using the attribute `<script type="module">`.
The browser automatically fetches and evaluates imports, then runs the script.
55
+
The browser automatically fetches and evaluates imported modules, and then runs the script.
54
56
55
57
## Core module features
56
58
@@ -60,7 +62,7 @@ There are core features, valid both for browser and server-side JavaScript.
60
62
61
63
### Always "use strict"
62
64
63
-
Modules always `use strict`. E.g. assigning to an undeclared variable will give an error.
65
+
Modules always `use strict`, by default. E.g. assigning to an undeclared variable will give an error.
64
66
65
67
```html run
66
68
<scripttype="module">
@@ -101,7 +103,7 @@ In the browser, independent top-level scope also exists for each `<script type="
101
103
</script>
102
104
```
103
105
104
-
If we really need to make a "global" in-browser variable, we can explicitly assign it to `window` and access as `window.user`. But that's an exception requiring a good reason.
106
+
If we really need to make a window-level global variable, we can explicitly assign it to `window` and access as `window.user`. But that's an exception requiring a good reason.
105
107
106
108
### A module code is evaluated only the first time when imported
107
109
@@ -229,11 +231,11 @@ You may want skip those for now if you're reading for the first time, or if you
229
231
Module scripts are *always* deferred, same effect as `defer` attribute (described in the chapter [](info:script-async-defer)), for both external and inline scripts.
- module scripts wait until the HTML document is fully ready.
234
-
- relative order is maintained: scripts that go first in the document, execute first.
234
+
- external module scripts `<script type="module" src="...">` don't block HTML processing, they load in parallel with other resources.
235
+
- module scripts wait until the HTML document is fully ready (even if they are tiny and load faster than HTML), and then run.
236
+
- relative order of scripts is maintained: scripts that go first in the document, execute first.
235
237
236
-
As a side-effect, module scripts always see HTML elements below them.
238
+
As a side-effect, module scripts always "see" the fully loaded HTML-page, including HTML elements below them.
237
239
238
240
For instance:
239
241
@@ -245,6 +247,8 @@ For instance:
245
247
// as modules are deferred, the script runs after the whole page is loaded
246
248
</script>
247
249
250
+
Compare to regular script below:
251
+
248
252
<script>
249
253
*!*
250
254
alert(typeof button); // Error: button is undefined, the script can't see elements below
@@ -259,7 +263,7 @@ Please note: the second script actually works before the first! So we'll see `un
259
263
260
264
That's because modules are deferred, so way wait for the document to be processed. The regular scripts runs immediately, so we saw its output first.
261
265
262
-
When using modules, we should be aware that HTML-document can show up before the JavaScript application is ready. Some functionality may not work yet. We should put transparent overlays or "loading indicators", or otherwise ensure that the visitor won't be confused because of it.
266
+
When using modules, we should be aware that HTML-page shows up as it loads, and JavaScript modules run after that, so the user may see the page before the JavaScript application is ready. Some functionality may not work yet. We should put transparent overlays or "loading indicators", or otherwise ensure that the visitor won't be confused by that.
263
267
264
268
### Async works on inline scripts
265
269
@@ -303,15 +307,15 @@ There are two notable differences of external module scripts:
303
307
304
308
### No "bare" modules allowed
305
309
306
-
In the browser, in scripts (not in HTML), `import` must get either a relative or absolute URL. Modules without any path are called "bare" modules. Such modules are not allowed in `import`.
310
+
In the browser, `import` must get either a relative or absolute URL. Modules without any path are called "bare" modules. Such modules are not allowed in `import`.
// the module must have a path, e.g. './sayHi.js' or wherever the module is
312
316
```
313
317
314
-
Certain environments, like Node.js or bundle tools allow bare modules, as they have own ways for finding modules and hooks to fine-tune them. But browsers do not support bare modules yet.
318
+
Certain environments, like Node.js or bundle tools allow bare modules, without any path, as they have own ways for finding modules and hooks to fine-tune them. But browsers do not support bare modules yet.
315
319
316
320
### Compatibility, "nomodule"
317
321
@@ -362,7 +366,7 @@ To summarize, the core concepts are:
362
366
1. A module is a file. To make `import/export` work, browsers need `<script type="module">`, that implies several differences:
363
367
- Deferred by default.
364
368
- Async works on inline scripts.
365
-
- External scripts need CORS headers.
369
+
- To load external scripts from another origin (domain/protocol/port), CORS headers are needed.
366
370
- Duplicate external scripts are ignored.
367
371
2. Modules have their own, local top-level scope and interchange functionality via `import/export`.
1. Yes, true. The element `elem.lastChild` is always the last one, it has no `nextSibling`, so if there are children, then yes.
2
-
2. No, wrong, because `elem.children[0]` is the first child among elements. But there may be non-element nodes before it. So `previousSibling` may be a text node.
1
+
1. Yes, true. The element `elem.lastChild` is always the last one, it has no `nextSibling`.
2
+
2. No, wrong, because `elem.children[0]` is the first child *among elements*. But there may exist non-element nodes before it. So `previousSibling` may be a text node. Also, if there are no children, then trying to access `elem.children[0]`
3
3
4
-
Please note that for both cases if there are no children, then there will be an error. For instance, if `elem.lastChild` is `null`, we can't access `elem.lastChild.nextSibling`.
4
+
Please note: for both cases if there are no children, then there will be an error.
5
+
6
+
If there are no children, `elem.lastChild` is `null`, so we can't access `elem.lastChild.nextSibling`. And the collection `elem.children` is empty (like an empty array `[]`).
Copy file name to clipboardExpand all lines: 2-ui/1-document/03-dom-navigation/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ libs:
7
7
8
8
# Walking the DOM
9
9
10
-
The DOM allows us to do anything with elements and their contents, but first we need to reach the corresponding DOM object, get it into a variable, and then we are able to modify it.
10
+
The DOM allows us to do anything with elements and their contents, but first we need to reach the corresponding DOM object.
11
11
12
12
All operations on the DOM start with the `document` object. From it we can access any node.
13
13
@@ -86,7 +86,7 @@ For instance, here `<body>` has children `<div>` and `<ul>` (and few blank text
86
86
</html>
87
87
```
88
88
89
-
...And if we ask for all descendants of `<body>`, then we get direct children `<div>`, `<ul>` and also more nested elements like `<li>` (being a child of `<ul>`) and `<b>` (being a child of `<li>`) -- the entire subtree.
89
+
...And all descendants of `<body>` are not only direct children `<div>`, `<ul>` but also more deeply nested elements, such as `<li>` (a child of `<ul>`) and `<b>` (a child of `<li>`) -- the entire subtree.
90
90
91
91
**The `childNodes` collection provides access to all child nodes, including text nodes.**
There are also properties with a value. For instance, Unicode "Script" (a writing system) can be Cyrillic, Greek, Arabic, Han (Chinese) etc, the [list is long]("https://en.wikipedia.org/wiki/Script_(Unicode)").
49
49
50
-
To search for certain scripts, we should supply `Script=<value>`, e.g. to search for cyrillic letters: `\p{sc=Cyrillic}`, for Chinese glyphs: `\p{sc=Han}`, etc:
50
+
To search for characters in certain scripts ("alphabets"), we should supply `Script=<value>`, e.g. to search for cyrillic letters: `\p{sc=Cyrillic}`, for Chinese glyphs: `\p{sc=Han}`, etc:
Let's make a "universal" regexp for `pattern:\w`, for any language. That task has a standard solution in many programming languages with unicode-aware regexps, e.g. Perl.
62
+
The pattern `pattern:\w` means "wordly characters", but doesn't work for languages that use non-Latin alphabets, such as Cyrillic and others. It's just a shorthand for `[a-zA-Z0-9_]`, so `pattern:\w+` won't find any Chinese words etc.
63
+
64
+
Let's make a "universal" regexp, that looks for wordly characters in any language. That's easy to do using Unicode properties:
0 commit comments