Skip to content

Commit 09aef05

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-ef8d5768
2 parents d3a2ddb + ef8d576 commit 09aef05

File tree

30 files changed

+290
-98
lines changed

30 files changed

+290
-98
lines changed

Diff for: 1-js/02-first-steps/04-variables/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Now, we can put some data into it by using the assignment operator `=`:
2424
let message;
2525

2626
*!*
27-
message = 'Hello'; // store the string
27+
message = 'Hello'; // store the string 'Hello' in the variable named message
2828
*/!*
2929
```
3030

Diff for: 1-js/02-first-steps/13-while-for/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ label: {
377377
}
378378
```
379379
380-
...Although, 99.9% of the time `break` used is inside loops, as we've seen in the examples above.
380+
...Although, 99.9% of the time `break` is used inside loops, as we've seen in the examples above.
381381
382382
A `continue` is only possible from inside a loop.
383383
````

Diff for: 1-js/02-first-steps/15-function-basics/article.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ In other words, to put these terms straight:
181181
182182
We declare functions listing their parameters, then call them passing arguments.
183183
184-
In the example above, one might say: "the function `sayMessage` is declared with two parameters, then called with two arguments: `from` and `"Hello"`".
184+
In the example above, one might say: "the function `showMessage` is declared with two parameters, then called with two arguments: `from` and `"Hello"`".
185185
186186
187187
## Default values
@@ -247,7 +247,7 @@ function showMessage(text) {
247247
showMessage(); // empty message
248248
```
249249
250-
...Or we could use the `??` operator:
250+
...Or we could use the `||` operator:
251251
252252
```js
253253
function showMessage(text) {

Diff for: 1-js/03-code-quality/06-polyfills/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Here, in this chapter, our purpose is to get the gist of how they work, and thei
2222

2323
## Transpilers
2424

25-
A [transpiler](https://en.wikipedia.org/wiki/Source-to-source_compiler) is a special piece of software that can parse ("read and understand") modern code, and rewrite it using older syntax constructs, so that the result would be the same.
25+
A [transpiler](https://en.wikipedia.org/wiki/Source-to-source_compiler) is a special piece of software that translates source code to another source code. It can parse ("read and understand") modern code and rewrite it using older syntax constructs, so that it'll also work in outdated engines.
2626

2727
E.g. JavaScript before year 2020 didn't have the "nullish coalescing operator" `??`. So, if a visitor uses an outdated browser, it may fail to understand the code like `height = height ?? 100`.
2828

Diff for: 1-js/04-object-basics/06-constructor-new/1-two-functions-one-object/task.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ importance: 2
44

55
# Two functions – one object
66

7-
Is it possible to create functions `A` and `B` such as `new A()==new B()`?
7+
Is it possible to create functions `A` and `B` so that `new A() == new B()`?
88

99
```js no-beautify
1010
function A() { ... }

Diff for: 1-js/04-object-basics/09-object-toprimitive/article.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ In case of such operations, objects are auto-converted to primitives, and then t
99

1010
That's an important limitation, as the result of `obj1 + obj2` can't be another object!
1111

12-
E.g. we can't make objects representing vectors or matrices (or archievements or whatever), add them and expect a "summed" object as the result. Such architectural feats are automatically "off the board".
12+
E.g. we can't make objects representing vectors or matrices (or achievements or whatever), add them and expect a "summed" object as the result. Such architectural feats are automatically "off the board".
1313

1414
So, because we can't do much here, there's no maths with objects in real projects. When it happens, it's usually because of a coding mistake.
1515

@@ -133,7 +133,7 @@ As we can see from the code, `user` becomes a self-descriptive string or a money
133133

134134
If there's no `Symbol.toPrimitive` then JavaScript tries to find methods `toString` and `valueOf`:
135135

136-
- For the "string" hint: `toString`, and if it doesn't exist, then `valueOf` (so `toString` has the priority for stirng conversions).
136+
- For the "string" hint: `toString`, and if it doesn't exist, then `valueOf` (so `toString` has the priority for string conversions).
137137
- For other hints: `valueOf`, and if it doesn't exist, then `toString` (so `valueOf` has the priority for maths).
138138

139139
Methods `toString` and `valueOf` come from ancient times. They are not symbols (symbols did not exist that long ago), but rather "regular" string-named methods. They provide an alternative "old-style" way to implement the conversion.
@@ -274,4 +274,4 @@ The conversion algorithm is:
274274

275275
In practice, it's often enough to implement only `obj.toString()` as a "catch-all" method for string conversions that should return a "human-readable" representation of an object, for logging or debugging purposes.
276276

277-
As for math operations, JavaScript doesn't provide a way to "override" them using methods, so real life projects rarely use them on objects.
277+
As for math operations, JavaScript doesn't provide a way to "override" them using methods, so real life projects rarely use them on objects.

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ alert( 7.3e9 ); // 7.3 billions (same as 7300000000 or 7_300_000_000)
3737
In other words, `e` multiplies the number by `1` with the given zeroes count.
3838

3939
```js
40-
1e3 = 1 * 1000 // e3 means *1000
41-
1.23e6 = 1.23 * 1000000 // e6 means *1000000
40+
1e3 === 1 * 1000; // e3 means *1000
41+
1.23e6 === 1.23 * 1000000; // e6 means *1000000
4242
```
4343

4444
Now let's write something very small. Say, 1 microsecond (one millionth of a second):
@@ -59,10 +59,10 @@ In other words, a negative number after `"e"` means a division by 1 with the giv
5959

6060
```js
6161
// -3 divides by 1 with 3 zeroes
62-
1e-3 = 1 / 1000 (=0.001)
62+
1e-3 === 1 / 1000; // 0.001
6363

6464
// -6 divides by 1 with 6 zeroes
65-
1.23e-6 = 1.23 / 1000000 (=0.00000123)
65+
1.23e-6 === 1.23 / 1000000; // 0.00000123
6666
```
6767

6868
### Hex, binary and octal numbers
@@ -118,6 +118,7 @@ Please note that two dots in `123456..toString(36)` is not a typo. If we want to
118118
If we placed a single dot: `123456.toString(36)`, then there would be an error, because JavaScript syntax implies the decimal part after the first dot. And if we place one more dot, then JavaScript knows that the decimal part is empty and now goes the method.
119119
120120
Also could write `(123456).toString(36)`.
121+
121122
```
122123

123124
## Rounding

Diff for: 1-js/05-data-types/05-array-methods/12-reduce-object/task.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ importance: 4
44

55
# Create keyed object from array
66

7-
Let's say we received an array of users in the form `{id:..., name:..., age... }`.
7+
Let's say we received an array of users in the form `{id:..., name:..., age:... }`.
88

99
Create a function `groupById(arr)` that creates an object from it, with `id` as the key, and array items as values.
1010

Diff for: 1-js/05-data-types/09-keys-values-entries/article.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Objects lack many methods that exist for arrays, e.g. `map`, `filter` and others
7777
If we'd like to apply them, then we can use `Object.entries` followed by `Object.fromEntries`:
7878

7979
1. Use `Object.entries(obj)` to get an array of key/value pairs from `obj`.
80-
2. Use array methods on that array, e.g. `map`.
80+
2. Use array methods on that array, e.g. `map`, to transform these key/value pairs.
8181
3. Use `Object.fromEntries(array)` on the resulting array to turn it back into an object.
8282

8383
For example, we have an object with prices, and would like to double them:
@@ -91,12 +91,13 @@ let prices = {
9191

9292
*!*
9393
let doublePrices = Object.fromEntries(
94-
// convert to array, map, and then fromEntries gives back the object
95-
Object.entries(prices).map(([key, value]) => [key, value * 2])
94+
// convert prices to array, map each key/value pair into another pair
95+
// and then fromEntries gives back the object
96+
Object.entries(prices).map(entry => [entry[0], entry[1] * 2])
9697
);
9798
*/!*
9899

99100
alert(doublePrices.meat); // 8
100-
```
101+
```
101102

102-
It may look difficult at first sight, but becomes easy to understand after you use it once or twice. We can make powerful chains of transforms this way.
103+
It may look difficult at first sight, but becomes easy to understand after you use it once or twice. We can make powerful chains of transforms this way.

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

+18-10
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ alert(Object.keys(user)); // name
194194

195195
The non-configurable flag (`configurable:false`) is sometimes preset for built-in objects and properties.
196196

197-
A non-configurable property can not be deleted.
197+
A non-configurable property can't be deleted, its attributes can't be modified.
198198

199199
For instance, `Math.PI` is non-writable, non-enumerable and non-configurable:
200200

@@ -214,20 +214,23 @@ alert( JSON.stringify(descriptor, null, 2 ) );
214214
So, a programmer is unable to change the value of `Math.PI` or overwrite it.
215215

216216
```js run
217-
Math.PI = 3; // Error
217+
Math.PI = 3; // Error, because it has writable: false
218218

219219
// delete Math.PI won't work either
220220
```
221221

222-
Making a property non-configurable is a one-way road. We cannot change it back with `defineProperty`.
222+
We also can't change `Math.PI` to be `writable` again:
223+
224+
```js run
225+
// Error, because of configurable: false
226+
Object.defineProperty(Math, "PI", { writable: true });
227+
```
223228

224-
To be precise, non-configurability imposes several restrictions on `defineProperty`:
225-
1. Can't change `configurable` flag.
226-
2. Can't change `enumerable` flag.
227-
3. Can't change `writable: false` to `true` (the other way round works).
228-
4. Can't change `get/set` for an accessor property (but can assign them if absent).
229+
There's absolutely nothing we can do with `Math.PI`.
229230

230-
**The idea of "configurable: false" is to prevent changes of property flags and its deletion, while allowing to change its value.**
231+
Making a property non-configurable is a one-way road. We cannot change it back with `defineProperty`.
232+
233+
**Please note: `configurable: false` prevents changes of property flags and its deletion, while allowing to change its value.**
231234

232235
Here `user.name` is non-configurable, but we can still change it (as it's writable):
233236

@@ -244,7 +247,7 @@ user.name = "Pete"; // works fine
244247
delete user.name; // Error
245248
```
246249

247-
And here we make `user.name` a "forever sealed" constant:
250+
And here we make `user.name` a "forever sealed" constant, just like the built-in `Math.PI`:
248251

249252
```js run
250253
let user = {
@@ -263,6 +266,11 @@ delete user.name;
263266
Object.defineProperty(user, "name", { value: "Pete" });
264267
```
265268

269+
```smart header="The only attribute change possible: writable true -> false"
270+
There's a minor exception about changing flags.
271+
272+
We can change `writable: true` to `false` for a non-configurable property, thus preventing its value modification (to add another layer of protection). Not the other way around though.
273+
```
266274

267275
## Object.defineProperties
268276

Diff for: 1-js/09-classes/04-private-protected-properties-methods/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class CoffeeMachine {
116116
let coffeeMachine = new CoffeeMachine(100);
117117

118118
// add water
119-
coffeeMachine.waterAmount = -10; // Error: Negative water
119+
coffeeMachine.waterAmount = -10; // _waterAmount will become 0, not -10
120120
```
121121

122122
Now the access is under control, so setting the water amount below zero becomes impossible.

Diff for: 1-js/10-error-handling/2-custom-errors/article.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Internally, we'll use `JSON.parse`. If it receives malformed `json`, then it thr
2121

2222
Our function `readUser(json)` will not only read JSON, but check ("validate") the data. If there are no required fields, or the format is wrong, then that's an error. And that's not a `SyntaxError`, because the data is syntactically correct, but another kind of error. We'll call it `ValidationError` and create a class for it. An error of that kind should also carry the information about the offending field.
2323

24-
Our `ValidationError` class should inherit from the built-in `Error` class.
24+
Our `ValidationError` class should inherit from the `Error` class.
2525

26-
That class is built-in, but here's its approximate code so we can understand what we're extending:
26+
The `Error` class is built-in, but here's its approximate code so we can understand what we're extending:
2727

2828
```js
2929
// The "pseudocode" for the built-in Error class defined by JavaScript itself
@@ -117,15 +117,15 @@ We could also look at `err.name`, like this:
117117
// instead of (err instanceof SyntaxError)
118118
} else if (err.name == "SyntaxError") { // (*)
119119
// ...
120-
```
120+
```
121121
122122
The `instanceof` version is much better, because in the future we are going to extend `ValidationError`, make subtypes of it, like `PropertyRequiredError`. And `instanceof` check will continue to work for new inheriting classes. So that's future-proof.
123123
124-
Also it's important that if `catch` meets an unknown error, then it rethrows it in the line `(**)`. The `catch` block only knows how to handle validation and syntax errors, other kinds (due to a typo in the code or other unknown ones) should fall through.
124+
Also it's important that if `catch` meets an unknown error, then it rethrows it in the line `(**)`. The `catch` block only knows how to handle validation and syntax errors, other kinds (caused by a typo in the code or other unknown reasons) should fall through.
125125
126126
## Further inheritance
127127
128-
The `ValidationError` class is very generic. Many things may go wrong. The property may be absent or it may be in a wrong format (like a string value for `age`). Let's make a more concrete class `PropertyRequiredError`, exactly for absent properties. It will carry additional information about the property that's missing.
128+
The `ValidationError` class is very generic. Many things may go wrong. The property may be absent or it may be in a wrong format (like a string value for `age` instead of a number). Let's make a more concrete class `PropertyRequiredError`, exactly for absent properties. It will carry additional information about the property that's missing.
129129
130130
```js run
131131
class ValidationError extends Error {

Diff for: 1-js/11-async/01-callbacks/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function loadScript(src) {
2828
}
2929
```
3030

31-
It appends to the document the new, dynamically created, tag `<script src="…">` with given `src`. The browser automatically starts loading it and executes when complete.
31+
It inserts into the document a new, dynamically created, tag `<script src="…">` with the given `src`. The browser automatically starts loading it and executes when complete.
3232

3333
We can use this function like this:
3434

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let promise = Promise.all([...promises...]);
1818

1919
`Promise.all` takes an array of promises (it technically can be any iterable, but is usually an array) and returns a new promise.
2020

21-
The new promise resolves when all listed promises are settled, and the array of their results becomes its result.
21+
The new promise resolves when all listed promises are resolved, and the array of their results becomes its result.
2222

2323
For instance, the `Promise.all` below settles after 3 seconds, and then its result is an array `[1, 2, 3]`:
2424

Diff for: 1-js/12-generators-iterators/1-generators/article.md

+22
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,28 @@ try {
448448

449449
If we don't catch the error there, then, as usual, it falls through to the outer calling code (if any) and, if uncaught, kills the script.
450450

451+
## generator.return
452+
453+
`generator.return(value)` finishes the generator execution and return the given `value`.
454+
455+
```js
456+
function* gen() {
457+
yield 1;
458+
yield 2;
459+
yield 3;
460+
}
461+
462+
const g = gen();
463+
464+
g.next(); // { value: 1, done: false }
465+
g.return('foo'); // { value: "foo", done: true }
466+
g.next(); // { value: undefined, done: true }
467+
```
468+
469+
If we again use `generator.return()` in a completed generator, it will return that value again ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/return)).
470+
471+
Often we don't use it, as most of time we want to get all returning values, but it can be useful when we want to stop generator in a specific condition.
472+
451473
## Summary
452474

453475
- Generators are created by generator functions `function* f(…) {…}`.

0 commit comments

Comments
 (0)