Merge branch 'refactor'

This commit is contained in:
Ilya Kantor 2019-04-02 14:02:03 +03:00
commit e22e971664
374 changed files with 2836 additions and 175 deletions

View file

@ -57,7 +57,7 @@ Even if we press `key:Shift+Enter` to input multiple lines, and put `use strict`
The reliable way to ensure `use strict` would be to input the code into console like this:
```
```js
(function() {
'use strict';

View file

@ -1,5 +1,5 @@
```js run no-beautify
function sortByName(arr) {
function sortByAge(arr) {
arr.sort((a, b) => a.age > b.age ? 1 : -1);
}
@ -7,11 +7,12 @@ let john = { name: "John", age: 25 };
let pete = { name: "Pete", age: 30 };
let mary = { name: "Mary", age: 28 };
let arr = [ john, pete, mary ];
let arr = [ pete, john, mary ];
sortByName(arr);
sortByAge(arr);
// now sorted is: [john, mary, pete]
alert(arr[0].name); // John
alert(arr[1].name); // Mary
alert(arr[2].name); // Pete
```

View file

@ -2,9 +2,9 @@ importance: 5
---
# Sort objects
# Sort users by age
Write the function `sortByName(users)` that gets an array of objects with the `age` property and sorts them by `age`.
Write the function `sortByAge(users)` that gets an array of objects with the `age` property and sorts them by `age`.
For instance:
@ -13,11 +13,12 @@ let john = { name: "John", age: 25 };
let pete = { name: "Pete", age: 30 };
let mary = { name: "Mary", age: 28 };
let arr = [ john, pete, mary ];
let arr = [ pete, john, mary ];
sortByName(arr);
sortByAge(arr);
// now: [john, mary, pete]
alert(arr[0].name); // John
alert(arr[1].name); // Mary
alert(arr[2].name); // Pete
```

View file

@ -5,7 +5,7 @@ let i = 0;
let start = Date.now();
let timer = setInterval(count, 0);
let timer = setInterval(count);
function count() {
@ -20,4 +20,3 @@ function count() {
}
```

View file

@ -18,7 +18,7 @@ function count() {
if (i == 1000000000) {
alert("Done in " + (Date.now() - start) + 'ms');
} else {
setTimeout(count, 0);
setTimeout(count);
}
// a piece of heavy job

View file

@ -15,7 +15,7 @@ These methods are not a part of JavaScript specification. But most environments
The syntax:
```js
let timerId = setTimeout(func|code, delay[, arg1, arg2...])
let timerId = setTimeout(func|code, [delay], [arg1], [arg2], ...)
```
Parameters:
@ -25,7 +25,7 @@ Parameters:
Usually, that's a function. For historical reasons, a string of code can be passed, but that's not recommended.
`delay`
: The delay before run, in milliseconds (1000 ms = 1 second).
: The delay before run, in milliseconds (1000 ms = 1 second), by default 0.
`arg1`, `arg2`...
: Arguments for the function (not supported in IE9-)
@ -110,7 +110,7 @@ For browsers, timers are described in the [timers section](https://www.w3.org/TR
The `setInterval` method has the same syntax as `setTimeout`:
```js
let timerId = setInterval(func|code, delay[, arg1, arg2...])
let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...)
```
All arguments have the same meaning. But unlike `setTimeout` it runs the function not only once, but regularly after the given interval of time.
@ -238,7 +238,7 @@ There's a side-effect. A function references the outer lexical environment, so,
## setTimeout(...,0)
There's a special use case: `setTimeout(func, 0)`.
There's a special use case: `setTimeout(func, 0)`, or just `setTimeout(func)`.
This schedules the execution of `func` as soon as possible. But scheduler will invoke it only after the current code is complete.
@ -247,7 +247,7 @@ So the function is scheduled to run "right after" the current code. In other wor
For instance, this outputs "Hello", then immediately "World":
```js run
setTimeout(() => alert("World"), 0);
setTimeout(() => alert("World"));
alert("Hello");
```
@ -260,7 +260,7 @@ There's a trick to split CPU-hungry tasks using `setTimeout`.
For instance, a syntax-highlighting script (used to colorize code examples on this page) is quite CPU-heavy. To highlight the code, it performs the analysis, creates many colored elements, adds them to the document -- for a big text that takes a lot. It may even cause the browser to "hang", which is unacceptable.
So we can split the long text into pieces. First 100 lines, then plan another 100 lines using `setTimeout(...,0)`, and so on.
So we can split the long text into pieces. First 100 lines, then plan another 100 lines using `setTimeout(..., 0)`, and so on.
For clarity, let's take a simpler example for consideration. We have a function to count from `1` to `1000000000`.
@ -303,7 +303,7 @@ function count() {
if (i == 1e9) {
alert("Done in " + (Date.now() - start) + 'ms');
} else {
setTimeout(count, 0); // schedule the new call (**)
setTimeout(count); // schedule the new call (**)
}
}
@ -338,7 +338,7 @@ function count() {
// move the scheduling at the beginning
if (i < 1e9 - 1e6) {
setTimeout(count, 0); // schedule the new call
setTimeout(count); // schedule the new call
}
do {
@ -371,8 +371,8 @@ setTimeout(function run() {
times.push(Date.now() - start); // remember delay from the previous call
if (start + 100 < Date.now()) alert(times); // show the delays after 100ms
else setTimeout(run, 0); // else re-schedule
}, 0);
else setTimeout(run); // else re-schedule
});
// an example of the output:
// 1,1,1,1,9,15,20,24,30,35,40,45,50,55,59,64,70,75,80,85,90,95,100
@ -430,7 +430,7 @@ And if we use `setTimeout` to split it into pieces then changes are applied in-b
} while (i % 1e3 != 0);
if (i < 1e9) {
setTimeout(count, 0);
setTimeout(count);
}
}

View file

@ -226,7 +226,7 @@ You may want skip those for now if you're reading for the first time, or if you
### Module scripts are deferred
Module scripts are *always* deferred, same effect as `defer` attribute (described in the chapter [](info:onload-ondomcontentloaded)), for both external and inline scripts.
Module scripts are *always* deferred, same effect as `defer` attribute (described in the chapter [](info:script-async-defer)), for both external and inline scripts.
In other words:
- external module scripts `<script type="module" src="...">` don't block HTML processing.