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/02-first-steps/15-function-basics/article.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -181,7 +181,7 @@ In other words, to put these terms straight:
181
181
182
182
We declare functions listing their parameters, then call them passing arguments.
183
183
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"`".
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/06-polyfills/article.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Here, in this chapter, our purpose is to get the gist of how they work, and thei
22
22
23
23
## Transpilers
24
24
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.
26
26
27
27
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`.
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/09-object-toprimitive/article.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ In case of such operations, objects are auto-converted to primitives, and then t
9
9
10
10
That's an important limitation, as the result of `obj1 + obj2` can't be another object!
11
11
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".
13
13
14
14
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.
15
15
@@ -133,7 +133,7 @@ As we can see from the code, `user` becomes a self-descriptive string or a money
133
133
134
134
If there's no `Symbol.toPrimitive` then JavaScript tries to find methods `toString` and `valueOf`:
135
135
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).
137
137
- For other hints: `valueOf`, and if it doesn't exist, then `toString` (so `valueOf` has the priority for maths).
138
138
139
139
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:
274
274
275
275
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.
276
276
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.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/article.md
+5-4
Original file line number
Diff line number
Diff line change
@@ -37,8 +37,8 @@ alert( 7.3e9 ); // 7.3 billions (same as 7300000000 or 7_300_000_000)
37
37
In other words, `e` multiplies the number by `1` with the given zeroes count.
38
38
39
39
```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
42
42
```
43
43
44
44
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
59
59
60
60
```js
61
61
// -3 divides by 1 with 3 zeroes
62
-
1e-3=1/1000 (=0.001)
62
+
1e-3===1/1000; //0.001
63
63
64
64
// -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
66
66
```
67
67
68
68
### 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
118
118
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.
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.
```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.
Copy file name to clipboardExpand all lines: 1-js/10-error-handling/2-custom-errors/article.md
+5-5
Original file line number
Diff line number
Diff line change
@@ -21,9 +21,9 @@ Internally, we'll use `JSON.parse`. If it receives malformed `json`, then it thr
21
21
22
22
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.
23
23
24
-
Our `ValidationError` class should inherit from the built-in `Error` class.
24
+
Our `ValidationError` class should inherit from the `Error` class.
25
25
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:
27
27
28
28
```js
29
29
// 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:
117
117
// instead of (err instanceof SyntaxError)
118
118
} elseif (err.name=="SyntaxError") { // (*)
119
119
// ...
120
-
```
120
+
```
121
121
122
122
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.
123
123
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.
125
125
126
126
## Further inheritance
127
127
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.
Copy file name to clipboardExpand all lines: 1-js/11-async/01-callbacks/article.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ function loadScript(src) {
28
28
}
29
29
```
30
30
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.
Copy file name to clipboardExpand all lines: 1-js/12-generators-iterators/1-generators/article.md
+22
Original file line number
Diff line number
Diff line change
@@ -448,6 +448,28 @@ try {
448
448
449
449
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.
450
450
451
+
## generator.return
452
+
453
+
`generator.return(value)` finishes the generator execution and return the given `value`.
454
+
455
+
```js
456
+
function*gen() {
457
+
yield1;
458
+
yield2;
459
+
yield3;
460
+
}
461
+
462
+
constg=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
+
451
473
## Summary
452
474
453
475
- Generators are created by generator functions `function* f(…) {…}`.
0 commit comments