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/06-advanced-functions/01-recursion/01-sum-to/solution.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,4 +37,4 @@ P.S. Naturally, the formula is the fastest solution. It uses only 3 operations f
37
37
38
38
The loop variant is the second in terms of speed. In both the recursive and the loop variant we sum the same numbers. But the recursion involves nested calls and execution stack management. That also takes resources, so it's slower.
39
39
40
-
P.P.S. Some engines support the "tail call" optimization: if a recursive call is the very last one in the function (like in `sumTo` above), then the outer function will not need to resume the execution, so the engine doesn't need to remember its execution context. That removes the burden on memory, so counting `sumTo(100000)` becomes possible. But if the JavaScript engine does not support tail call optimization (most of them don't), there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.
40
+
P.P.S. Some engines support the "tail call" optimization: if a recursive call is the very last one in the function, with no other calculations performed, then the outer function will not need to resume the execution, so the engine doesn't need to remember its execution context. That removes the burden on memory. But if the JavaScript engine does not support tail call optimization (most of them don't), there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/01-recursion/article.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ When `pow(x, n)` is called, the execution splits into two branches:
61
61
if n==1 = x
62
62
/
63
63
pow(x, n) =
64
-
\
64
+
\
65
65
else = x * pow(x, n - 1)
66
66
```
67
67
@@ -285,7 +285,7 @@ The iterative `pow` uses a single context changing `i` and `result` in the proce
285
285
286
286
**Any recursion can be rewritten as a loop. The loop variant usually can be made more effective.**
287
287
288
-
...But sometimes the rewrite is non-trivial, especially when function uses different recursive subcalls depending on conditions and merges their results or when the branching is more intricate. And the optimization may be unneeded and totally not worth the efforts.
288
+
...But sometimes the rewrite is non-trivial, especially when a function uses different recursive subcalls depending on conditions and merges their results or when the branching is more intricate. And the optimization may be unneeded and totally not worth the efforts.
289
289
290
290
Recursion can give a shorter code, easier to understand and support. Optimizations are not required in every place, mostly we need a good code, that's why it's used.
291
291
@@ -535,7 +535,7 @@ Terms:
535
535
list = { value, next -> list }
536
536
```
537
537
538
-
Trees like HTML elements tree or the department tree from this chapter are also naturally recursive: they branch and every branch can have other branches.
538
+
Trees like HTML elements tree or the department tree from this chapter are also naturally recursive: they have branches and every branch can have other branches.
539
539
540
540
Recursive functions can be used to walk them as we've seen in the `sumSalary` example.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/02-rest-parameters-spread/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ function sum(a, b) {
23
23
alert( sum(1, 2, 3, 4, 5) );
24
24
```
25
25
26
-
There will be no error because of "excessive" arguments. But of course in the result only the first two will be counted.
26
+
There will be no error because of "excessive" arguments. But of course in the result only the first two will be counted, so the result in the code above is `3`.
27
27
28
28
The rest of the parameters can be included in the function definition by using three dots `...` followed by the name of the array that will contain them. The dots literally mean "gather the remaining parameters into an array".
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/05-global-object/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ var gVar = 5;
25
25
alert(window.gVar); // 5 (became a property of the global object)
26
26
```
27
27
28
-
The same effect have function declarations (statements with `function` keyword in the main code flow, not function expressions).
28
+
Function declarations have the same effect (statements with `function` keyword in the main code flow, not function expressions).
29
29
30
30
Please don't rely on that! This behavior exists for compatibility reasons. Modern scripts use [JavaScript modules](info:modules) where such a thing doesn't happen.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/08-settimeout-setinterval/article.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ Usually, that's a function. For historical reasons, a string of code can be pass
27
27
: The delay before run, in milliseconds (1000 ms = 1 second), by default 0.
28
28
29
29
`arg1`, `arg2`...
30
-
: Arguments for the function (not supported in IE9-)
30
+
: Arguments for the function
31
31
32
32
For instance, this code calls `sayHi()` after one second:
33
33
@@ -102,7 +102,7 @@ As we can see from `alert` output, in a browser the timer identifier is a number
102
102
103
103
Again, there is no universal specification for these methods, so that's fine.
104
104
105
-
For browsers, timers are described in the [timers section](https://www.w3.org/TR/html5/webappapis.html#timers) of HTML5 standard.
105
+
For browsers, timers are described in the [timers section](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) of HTML Living Standard.
For `setInterval` the function stays in memory until `clearInterval` is called.
234
234
235
-
There's a side-effect. A function references the outer lexical environment, so, while it lives, outer variables live too. They may take much more memory than the function itself. So when we don't need the scheduled function anymore, it's better to cancel it, even if it's very small.
235
+
There's a sideeffect. A function references the outer lexical environment, so, while it lives, outer variables live too. They may take much more memory than the function itself. So when we don't need the scheduled function anymore, it's better to cancel it, even if it's very small.
236
236
````
237
237
238
238
## Zero delay setTimeout
@@ -256,7 +256,7 @@ The first line "puts the call into calendar after 0ms". But the scheduler will o
256
256
There are also advanced browser-related use cases of zero-delay timeout, that we'll discuss in the chapter <info:event-loop>.
257
257
258
258
````smart header="Zero delay is in fact not zero (in a browser)"
259
-
In the browser, there's a limitation of how often nested timers can run. The [HTML5 standard](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) says: "after five nested timers, the interval is forced to be at least 4 milliseconds.".
259
+
In the browser, there's a limitation of how often nested timers can run. The [HTML Living Standard](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) says: "after five nested timers, the interval is forced to be at least 4 milliseconds.".
260
260
261
261
Let's demonstrate what it means with the example below. The `setTimeout` call in it re-schedules itself with zero delay. Each call remembers the real time from the previous one in the `times` array. What do the real delays look like? Let's see:
262
262
@@ -297,6 +297,6 @@ Please note that all scheduling methods do not *guarantee* the exact delay.
297
297
For example, the in-browser timer may slow down for a lot of reasons:
298
298
- The CPU is overloaded.
299
299
- The browser tab is in the background mode.
300
-
- The laptop is on battery.
300
+
- The laptop is on battery saving mode.
301
301
302
302
All that may increase the minimal timer resolution (the minimal delay) to 300ms or even 1000ms depending on the browser and OS-level performance settings.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/10-bind/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -202,7 +202,7 @@ for (let key in user) {
202
202
}
203
203
```
204
204
205
-
JavaScript libraries also provide functions for convenient mass binding , e.g. [_.bindAll(object, methodNames)](http://lodash.com/docs#bindAll) in lodash.
205
+
JavaScript libraries also provide functions for convenient mass binding , e.g. [_.bindAll(object, methodNames)](https://lodash.com/docs#bindAll) in lodash.
0 commit comments