Skip to content

Commit c5764ca

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-5e9eca37
2 parents 20bd6db + 5e9eca3 commit c5764ca

File tree

28 files changed

+123
-139
lines changed

28 files changed

+123
-139
lines changed

1-js/04-object-basics/04-object-methods/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ user.hi(); // John (the simple call works)
257257
*/!*
258258
```
259259
260-
On the last line there is a conditinal operator that chooses either `user.hi` or `user.bye`. In this case the result is `user.hi`.
260+
On the last line there is a conditional operator that chooses either `user.hi` or `user.bye`. In this case the result is `user.hi`.
261261
262-
Then the method is immediately called with parentheses `()`. But it doesn't work right!
262+
Then the method is immediately called with parentheses `()`. But it doesn't work correctly!
263263
264264
As you can see, the call results in an error, because the value of `"this"` inside the call becomes `undefined`.
265265

1-js/04-object-basics/05-object-toprimitive/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
What happens when objects are added `obj1 + obj2`, subtracted `obj1 - obj2` or printed using `alert(obj)`?
55

6-
In that case objects are auto-converted to primitives, and then the operation is carried out.
6+
In that case, objects are auto-converted to primitives, and then the operation is carried out.
77

88
In the chapter <info:type-conversions> we've seen the rules for numeric, string and boolean conversions of primitives. But we left a gap for objects. Now, as we know about methods and symbols it becomes possible to fill it.
99

@@ -138,7 +138,7 @@ alert(+user); // valueOf -> 1000
138138
alert(user + 500); // valueOf -> 1500
139139
```
140140

141-
Often we want a single "catch-all" place to handle all primitive conversions. In this case we can implement `toString` only, like this:
141+
Often we want a single "catch-all" place to handle all primitive conversions. In this case, we can implement `toString` only, like this:
142142

143143
```js run
144144
let user = {

1-js/06-advanced-functions/03-closure/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ function f() {
599599
}
600600

601601
let g = f(); // while g is alive
602-
// there corresponding Lexical Environment lives
602+
// their corresponding Lexical Environment lives
603603

604604
g = null; // ...and now the memory is cleaned up
605605
```

1-js/06-advanced-functions/04-var/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ alert(phrase); // Error, phrase is not defined
3131

3232
## "var" has no block scope
3333

34-
`var` variables are either function-wide or global, they are visible through blocks.
34+
Variables, declared with `var`, are either function-wide or global. They are visible through blocks.
3535

3636
For instance:
3737

@@ -45,7 +45,7 @@ alert(test); // true, the variable lives after if
4545
*/!*
4646
```
4747

48-
`var` ignores code blocks, so we've got a global variable `test`.
48+
As `var` ignores code blocks, we've got a global variable `test`.
4949

5050
If we used `let test` instead of `var test`, then the variable would only be visible inside `if`:
5151

1-js/06-advanced-functions/08-settimeout-setinterval/article.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ There are two methods for it:
99

1010
These methods are not a part of JavaScript specification. But most environments have the internal scheduler and provide these methods. In particular, they are supported in all browsers and Node.js.
1111

12-
1312
## setTimeout
1413

1514
The syntax:
@@ -291,9 +290,9 @@ For server-side JavaScript, that limitation does not exist, and there exist othe
291290
- To cancel the execution, we should call `clearInterval/clearTimeout` with the value returned by `setInterval/setTimeout`.
292291
- Nested `setTimeout` calls is a more flexible alternative to `setInterval`. Also they can guarantee the minimal time *between* the executions.
293292
- Zero delay scheduling with `setTimeout(func, 0)` (the same as `setTimeout(func)`) is used to schedule the call "as soon as possible, but after the current code is complete".
294-
- The browsere ensures that for five or more nested call of `setTimeout`, or for zero-delay `setInterval`, the real delay between calls is at least 4ms. That's for historical reasons.
293+
- The browser limits the minimal delay for five or more nested call of `setTimeout` or for `setInterval` (after 5th call) to 4ms. That's for historical reasons.
295294

296-
Please note that all scheduling methods do not *guarantee* the exact delay. We should not rely on that in the scheduled code.
295+
Please note that all scheduling methods do not *guarantee* the exact delay.
297296

298297
For example, the in-browser timer may slow down for a lot of reasons:
299298
- The CPU is overloaded.

1-js/07-object-properties/01-property-descriptors/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ user.name = "Pete"; // Error: Cannot assign to read only property 'name'...
122122

123123
Now no one can change the name of our user, unless they apply their own `defineProperty` to override ours.
124124

125-
```smart header="Errors appear only in use strict"
125+
```smart header="Errors appear only in strict mode"
126126
In the non-strict mode, no errors occur when writing to read-only properties and such. But the operation still won't succeed. Flag-violating actions are just silently ignored in non-strict.
127127
```
128128

1-js/08-prototypes/04-prototype-methods/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ let clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescr
194194
- [Object.getOwnPropertySymbols(obj)](mdn:js/Object/getOwnPropertySymbols) -- returns an array of all own symbolic keys.
195195
- [Object.getOwnPropertyNames(obj)](mdn:js/Object/getOwnPropertyNames) -- returns an array of all own string keys.
196196
- [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) -- returns an array of all own keys.
197-
- [obj.hasOwnProperty(key)](mdn:js/Object/hasOwnProperty): it returns `true` if `obj` has its own (not inherited) keytt named `key`.
197+
- [obj.hasOwnProperty(key)](mdn:js/Object/hasOwnProperty): it returns `true` if `obj` has its own (not inherited) key named `key`.
198198

199199
We also made it clear that `__proto__` is a getter/setter for `[[Prototype]]` and resides in `Object.prototype`, just as other methods.
200200

1-js/09-classes/05-extend-natives/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Even more, we can customize that behavior.
3434

3535
We can add a special static getter `Symbol.species` to the class. If exists, it should return the constructor that JavaScript will use internally to create new entities in `map`, `filter` and so on.
3636

37-
If we'd like built-in methods like `map`, `filter` will return regular arrays, we can return `Array` in `Symbol.species`, like here:
37+
If we'd like built-in methods like `map` or `filter` to return regular arrays, we can return `Array` in `Symbol.species`, like here:
3838

3939
```js run
4040
class PowerArray extends Array {
@@ -79,7 +79,7 @@ So, if `Rabbit extends Animal`, then:
7979

8080
That's thoroughly explained in the chapter [](info:static-properties-methods#statics-and-inheritance).
8181

82-
But built-in classes are an exception. They don't inherit statics `(1)` from each other.
82+
But built-in classes are an exception. They don't inherit statics from each other.
8383

8484
For example, both `Array` and `Date` inherit from `Object`, so their instances have methods from `Object.prototype`. But `Array.[[Prototype]]` does not point to `Object`. So there's `Object.keys()`, but not `Array.keys()` and `Date.keys()`.
8585

1-js/09-classes/06-instanceof/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ alert( arr instanceof Object ); // true
4646

4747
Please note that `arr` also belongs to the `Object` class. That's because `Array` prototypally inherits from `Object`.
4848

49-
The `instanceof` operator examines the prototype chain for the check, but we can set a custom logic the static method `Symbol.hasInstance`.
49+
The `instanceof` operator examines the prototype chain for the check, but we can set a custom logic in the static method `Symbol.hasInstance`.
5050

5151
The algorithm of `obj instanceof Class` works roughly as follows:
5252

1-js/11-async/05-promise-api/article.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Promise.all(requests)
112112
));
113113
```
114114

115-
A bigger example with fetching user information for an array of github users by their names (or we could fetch an array of goods by their ids, the logic is same):
115+
A bigger example with fetching user information for an array of GitHub users by their names (we could fetch an array of goods by their ids, the logic is same):
116116

117117
```js run
118118
let names = ['iliakan', 'remy', 'jeresig'];
@@ -134,7 +134,7 @@ Promise.all(requests)
134134
.then(users => users.forEach(user => alert(user.name)));
135135
```
136136

137-
**If any of the promises is rejected, `Promise.all` immediately rejects with that error.**
137+
**If any of the promises is rejected, the promise returned by `Promise.all` immediately rejects with that error.**
138138

139139
For instance:
140140

@@ -155,10 +155,10 @@ If one promise rejects, `Promise.all` immediately rejects, completely forgetting
155155
156156
For example, if there are multiple `fetch` calls, like in the example above, and one fails, other ones will still continue to execute, but `Promise.all` don't watch them any more. They will probably settle, but the result will be ignored.
157157
158-
`Promise.all` does nothing to cancel them, as there's no concept of "cancellation" in promises. In [another chapter](fetch-abort) we'll cover `AbortController` that aims to help with that, but it's not a part of the Promise API.
158+
`Promise.all` does nothing to cancel them, as there's no concept of "cancellation" in promises. In [another chapter](info:fetch-abort) we'll cover `AbortController` that can help with that, but it's not a part of the Promise API.
159159
```
160160

161-
````smart header="`Promise.all(...)` allows non-promise items in `iterable`"
161+
````smart header="`Promise.all(iterable)` allows non-promise \"regular\" values in `iterable`"
162162
Normally, `Promise.all(...)` accepts an iterable (in most cases an array) of promises. But if any of those objects is not a promise, it's wrapped in `Promise.resolve`.
163163

164164
For instance, here the results are `[1, 2, 3]`:
@@ -173,8 +173,7 @@ Promise.all([
173173
]).then(alert); // 1, 2, 3
174174
```
175175

176-
So we are able to pass non-promise values to `Promise.all` where convenient.
177-
176+
So we are able to pass ready values to `Promise.all` where convenient.
178177
````
179178
180179
## Promise.allSettled
@@ -289,4 +288,4 @@ There are 5 static methods of `Promise` class:
289288
- `value` (if fulfilled) or `reason` (if rejected).
290289
5. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome.
291290
292-
Of these five, `Promise.all/allSettled` are the most common in practice.
291+
Of these five, `Promise.all` is probably the most common in practice.

1-js/11-async/07-microtask-queue/article.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ That's strange, because the promise is definitely done from the beginning.
2121

2222
Why did the `.then` trigger afterwards? What's going on?
2323

24-
## Microtasks
24+
## Microtasks queue
2525

2626
Asynchronous tasks need proper management. For that, the standard specifies an internal queue `PromiseJobs`, more often referred to as "microtask queue" (v8 term).
2727

@@ -68,7 +68,7 @@ let promise = Promise.reject(new Error("Promise Failed!"));
6868
promise.catch(err => alert('caught'));
6969
*/!*
7070

71-
// no error, all quiet
71+
// doesn't run: error handled
7272
window.addEventListener('unhandledrejection', event => alert(event.reason));
7373
```
7474

@@ -93,13 +93,13 @@ setTimeout(() => promise.catch(err => alert('caught')), 1000);
9393
window.addEventListener('unhandledrejection', event => alert(event.reason));
9494
```
9595

96-
Now, if you run it, we'll see `Promise Failed!` message first, and then `caught`.
96+
Now, if you run it, we'll see `Promise Failed!` message first, and then `caught`.
9797

98-
If we didn't know about microtasks, we could wonder: "Why did `unhandledrejection` happen? We did catch the error!".
98+
If we didn't know about microtasks queue, we could wonder: "Why did `unhandledrejection` handler run? We did catch the error!".
9999

100-
But now we do know that `unhandledrejection` is generated when the microtask queue is complete: the engine examines promises and, if any of them is in "rejected" state, then the event triggers.
100+
But now we understand that `unhandledrejection` is generated when the microtask queue is complete: the engine examines promises and, if any of them is in "rejected" state, then the event triggers.
101101

102-
...By the way, the `.catch` added by `setTimeout` also triggers, of course it does, but later, after `unhandledrejection` has already occurred.
102+
In the example above, `.catch` added by `setTimeout` also triggers, but later, after `unhandledrejection` has already occurred, so that doesn't change anything.
103103

104104
## Summary
105105

2-ui/1-document/09-size-and-scroll/article.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ So, without scrollbar the content width would be `300px`, but if the scrollbar i
3939
```
4040

4141
```smart header="The `padding-bottom` area may be filled with text"
42-
Usually paddings are shown empty on illustrations, but if there's a lot of text in the element and it overflows, then browsers show the "overflowing" text at `padding-bottom`, so you can see that in examples. Still, the padding is set in further examples, unless explicitly specified otherwise.
42+
Usually paddings are shown empty on illustrations, but if there's a lot of text in the element and it overflows, then browsers show the "overflowing" text at `padding-bottom`.
43+
44+
That's a note to avoid confusion, as `padding-bottom` is set in further examples, unless explicitly specified otherwise.
4345
```
4446
4547
## Geometry
@@ -66,7 +68,7 @@ That's the nearest ancestor, that satisfies following conditions:
6668
2. or `<td>`, `<th>`, `<table>`,
6769
2. or `<body>`.
6870
69-
In most practical cases `offsetParent` is exactly the nearest ancestor, that is CSS-positioned. And `offsetLeft/offsetTop` provide x/y coordinates relative to its upper-left corner.
71+
Properties `offsetLeft/offsetTop` provide x/y coordinates relative to its upper-left corner.
7072
7173
In the example below the inner `<div>` has `<main>` as `offsetParent` and `offsetLeft/offsetTop` shifts from its upper-left corner (`180`):
7274
@@ -105,7 +107,7 @@ For our sample element:
105107
- `offsetWidth = 390` -- the outer width, can be calculated as inner CSS-width (`300px`) plus paddings (`2 * 20px`) and borders (`2 * 25px`).
106108
- `offsetHeight = 290` -- the outer height.
107109

108-
````smart header="Geometry properties for not displayed elements are zero/null"
110+
````smart header="Geometry properties are zero/null for elements that are not displayed"
109111
Geometry properties are calculated only for displayed elements.
110112
111113
If an element (or any of its ancestors) has `display:none` or is not in the document, then all geometry properties are zero (or `null` if that's `offsetParent`).

2-ui/2-events/02-bubbling-and-capturing/article.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Here's the picture of a click on `<td>` inside a table, taken from the specifica
130130

131131
![](eventflow.png)
132132

133-
That is: for a click on `<td>` the event first goes through the ancestors chain down to the element (capturing), then it reaches the target, and then it goes up (bubbles), calling handlers on its way.
133+
That is: for a click on `<td>` the event first goes through the ancestors chain down to the element (capturing phase), then it reaches the target and triggers there (target phase), and then it goes up (bubbling phase), calling handlers on its way.
134134

135135
**Before we only talked about bubbling, because the capturing phase is rarely used. Normally it is invisible to us.**
136136

@@ -149,6 +149,7 @@ There are two possible values of the `capture` option:
149149
- If it's `false` (default), then the handler is set on the bubbling phase.
150150
- If it's `true`, then the handler is set on the capturing phase.
151151

152+
152153
Note that while formally there are 3 phases, the 2nd phase ("target phase": the event reached the element) is not handled separately: handlers on both capturing and bubbling phases trigger at that phase.
153154

154155
Let's see both capturing and bubbling in action:
@@ -179,30 +180,39 @@ The code sets click handlers on *every* element in the document to see which one
179180

180181
If you click on `<p>`, then the sequence is:
181182

182-
1. `HTML` -> `BODY` -> `FORM` -> `DIV` -> `P` (capturing phase, the first listener), and then:
183-
2. `P` -> `DIV` -> `FORM` -> `BODY` -> `HTML` (bubbling phase, the second listener).
184-
185-
Please note that `P` shows up two times: at the end of capturing and at the start of bubbling.
183+
1. `HTML` -> `BODY` -> `FORM` -> `DIV` (capturing phase, the first listener):
184+
2. `P` (target phrase, triggers two times, as we've set two listeners: capturing and bubbling)
185+
3. `DIV` -> `FORM` -> `BODY` -> `HTML` (bubbling phase, the second listener).
186186

187187
There's a property `event.eventPhase` that tells us the number of the phase on which the event was caught. But it's rarely used, because we usually know it in the handler.
188188

189189
```smart header="To remove the handler, `removeEventListener` needs the same phase"
190190
If we `addEventListener(..., true)`, then we should mention the same phase in `removeEventListener(..., true)` to correctly remove the handler.
191191
```
192192
193+
````smart header="Listeners on same element and same phase run in their set order"
194+
If we have multiple event handlers on the same phase, assigned to the same element with `addEventListener`, they run in the same order as they are created:
195+
196+
```js
197+
elem.addEventListener("click", e => alert(1)); // guaranteed to trigger first
198+
elem.addEventListener("click", e => alert(2));
199+
```
200+
````
201+
202+
193203
## Summary
194204
195-
The event handling process:
205+
When an event happens -- the most nested element where it happens gets labeled as the "target element" (`event.target`).
196206
197-
- When an event happens -- the most nested element where it happens gets labeled as the "target element" (`event.target`).
198-
- Then the event first moves from the document root down to the `event.target`, calling handlers assigned with `addEventListener(...., true)` on the way (`true` is a shorthand for `{capture: true}`).
199-
- Then the event moves from `event.target` up to the root, calling handlers assigned using `on<event>` and `addEventListener` without the 3rd argument or with the 3rd argument `false`.
207+
- Then the event moves down from the document root to `event.target`, calling handlers assigned with `addEventListener(...., true)` on the way (`true` is a shorthand for `{capture: true}`).
208+
- Then handlers are called on the target element itself.
209+
- Then the event bubbles up from `event.target` up to the root, calling handlers assigned using `on<event>` and `addEventListener` without the 3rd argument or with the 3rd argument `false/{capture:false}`.
200210
201211
Each handler can access `event` object properties:
202212
203213
- `event.target` -- the deepest element that originated the event.
204214
- `event.currentTarget` (=`this`) -- the current element that handles the event (the one that has the handler on it)
205-
- `event.eventPhase` -- the current phase (capturing=1, bubbling=3).
215+
- `event.eventPhase` -- the current phase (capturing=1, target=2, bubbling=3).
206216
207217
Any event handler can stop the event by calling `event.stopPropagation()`, but that's not recommended, because we can't really be sure we won't need it above, maybe for completely different things.
208218

2-ui/4-forms-controls/3-events-change-input/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ For other elements: `select`, `input type=checkbox/radio` it triggers right afte
2929

3030
## Event: input
3131

32-
The `input` event triggers every time after a value is modified.
32+
The `input` event triggers every time after a value is modified by the user.
3333

3434
Unlike keyboard events, it triggers on any value change, even those that does not involve keyboard actions: pasting with a mouse or using speech recognition to dictate the text.
3535

2-ui/5-loading/01-onload-ondomcontentloaded/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ window.addEventListener("unload", function() {
150150
```
151151
152152
- The request is sent as POST.
153-
- We can send not only a string, but also forms and other formats, as described in the chapter <info:fetch-basics>, but usually it's a stringified object.
153+
- We can send not only a string, but also forms and other formats, as described in the chapter <info:fetch>, but usually it's a stringified object.
154154
- The data is limited by 64kb.
155155
156156
When the `sendBeacon` request is finished, the browser probably has already left the document, so there's no way to get server response (which is usually empty for analytics).
157157
158-
There's also a `keepalive` flag for doing such "after-page-left" requests in [fetch](info:fetch-basics) method for generic network requests. You can find more information in the chapter <info:fetch-api>.
158+
There's also a `keepalive` flag for doing such "after-page-left" requests in [fetch](info:fetch) method for generic network requests. You can find more information in the chapter <info:fetch-api>.
159159
160160
161161
If we want to cancel the transition to another page, we can't do it here. But we can use another event -- `onbeforeunload`.

2-ui/99-ui-misc/01-mutation-observer/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ let hello = "world";
130130

131131
Everything's simple so far, right? There are `<pre>` code snippets in HTML, we highlight them.
132132

133-
Now let's go on. Let's say we're going to dynamically fetch materials from a server. We'll study methods for that [later in the tutorial](info:fetch-basics). For now it only matters that we fetch an HTML article from a webserver and display it on demand:
133+
Now let's go on. Let's say we're going to dynamically fetch materials from a server. We'll study methods for that [later in the tutorial](info:fetch). For now it only matters that we fetch an HTML article from a webserver and display it on demand:
134134

135135
```js
136136
let article = /* fetch new content from server */

0 commit comments

Comments
 (0)