commit
1dc6dfbc7c
3 changed files with 9 additions and 9 deletions
|
@ -196,9 +196,9 @@ So the single `callback` function is used both for reporting errors and passing
|
||||||
|
|
||||||
## Pyramid of Doom
|
## Pyramid of Doom
|
||||||
|
|
||||||
From the first look, it's a viable way of asynchronous coding. And indeed it is. For one or maybe two nested calls it looks fine.
|
At first glance, it looks like a viable approach to asynchronous coding. And indeed it is. For one or maybe two nested calls it looks fine.
|
||||||
|
|
||||||
But for multiple asynchronous actions that follow one after another we'll have code like this:
|
But for multiple asynchronous actions that follow one after another, we'll have code like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
loadScript('1.js', function(error, script) {
|
loadScript('1.js', function(error, script) {
|
||||||
|
@ -229,8 +229,8 @@ loadScript('1.js', function(error, script) {
|
||||||
```
|
```
|
||||||
|
|
||||||
In the code above:
|
In the code above:
|
||||||
1. We load `1.js`, then if there's no error.
|
1. We load `1.js`, then if there's no error...
|
||||||
2. We load `2.js`, then if there's no error.
|
2. We load `2.js`, then if there's no error...
|
||||||
3. We load `3.js`, then if there's no error -- do something else `(*)`.
|
3. We load `3.js`, then if there's no error -- do something else `(*)`.
|
||||||
|
|
||||||
As calls become more nested, the code becomes deeper and increasingly more difficult to manage, especially if we have real code instead of `...` that may include more loops, conditional statements and so on.
|
As calls become more nested, the code becomes deeper and increasingly more difficult to manage, especially if we have real code instead of `...` that may include more loops, conditional statements and so on.
|
||||||
|
@ -299,7 +299,7 @@ function step3(error, script) {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
See? It does the same, and there's no deep nesting now because we made every action a separate top-level function.
|
See? It does the same thing, and there's no deep nesting now because we made every action a separate top-level function.
|
||||||
|
|
||||||
It works, but the code looks like a torn apart spreadsheet. It's difficult to read, and you probably noticed that one needs to eye-jump between pieces while reading it. That's inconvenient, especially if the reader is not familiar with the code and doesn't know where to eye-jump.
|
It works, but the code looks like a torn apart spreadsheet. It's difficult to read, and you probably noticed that one needs to eye-jump between pieces while reading it. That's inconvenient, especially if the reader is not familiar with the code and doesn't know where to eye-jump.
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ There are two ways to tell the browser we don't want it to act:
|
||||||
- The main way is to use the `event` object. There's a method `event.preventDefault()`.
|
- The main way is to use the `event` object. There's a method `event.preventDefault()`.
|
||||||
- If the handler is assigned using `on<event>` (not by `addEventListener`), then returning `false` also works the same.
|
- If the handler is assigned using `on<event>` (not by `addEventListener`), then returning `false` also works the same.
|
||||||
|
|
||||||
In this HTML a click on a link doesn't lead to navigation, browser doesn't do anything:
|
In this HTML, a click on a link doesn't lead to navigation; the browser doesn't do anything:
|
||||||
|
|
||||||
```html autorun height=60 no-beautify
|
```html autorun height=60 no-beautify
|
||||||
<a href="/" onclick="return false">Click here</a>
|
<a href="/" onclick="return false">Click here</a>
|
||||||
|
@ -96,7 +96,7 @@ That's because the browser action is canceled on `mousedown`. The focusing is st
|
||||||
|
|
||||||
The optional `passive: true` option of `addEventListener` signals the browser that the handler is not going to call `preventDefault()`.
|
The optional `passive: true` option of `addEventListener` signals the browser that the handler is not going to call `preventDefault()`.
|
||||||
|
|
||||||
Why that may be needed?
|
Why might that be needed?
|
||||||
|
|
||||||
There are some events like `touchmove` on mobile devices (when the user moves their finger across the screen), that cause scrolling by default, but that scrolling can be prevented using `preventDefault()` in the handler.
|
There are some events like `touchmove` on mobile devices (when the user moves their finger across the screen), that cause scrolling by default, but that scrolling can be prevented using `preventDefault()` in the handler.
|
||||||
|
|
||||||
|
|
|
@ -39,9 +39,9 @@ In cases when a single action initiates multiple events, their order is fixed. T
|
||||||
```online
|
```online
|
||||||
Click the button below and you'll see the events. Try double-click too.
|
Click the button below and you'll see the events. Try double-click too.
|
||||||
|
|
||||||
On the teststand below all mouse events are logged, and if there is more than a 1 second delay between them they are separated by a horizontal ruler.
|
On the teststand below, all mouse events are logged, and if there is more than a 1 second delay between them, they are separated by a horizontal rule.
|
||||||
|
|
||||||
Also we can see the `button` property that allows to detect the mouse button, it's explained below.
|
Also, we can see the `button` property that allows us to detect the mouse button; it's explained below.
|
||||||
|
|
||||||
<input onmousedown="return logMouse(event)" onmouseup="return logMouse(event)" onclick="return logMouse(event)" oncontextmenu="return logMouse(event)" ondblclick="return logMouse(event)" value="Click me with the right or the left mouse button" type="button"> <input onclick="logClear('test')" value="Clear" type="button"> <form id="testform" name="testform"> <textarea style="font-size:12px;height:150px;width:360px;"></textarea></form>
|
<input onmousedown="return logMouse(event)" onmouseup="return logMouse(event)" onclick="return logMouse(event)" oncontextmenu="return logMouse(event)" ondblclick="return logMouse(event)" value="Click me with the right or the left mouse button" type="button"> <input onclick="logClear('test')" value="Clear" type="button"> <form id="testform" name="testform"> <textarea style="font-size:12px;height:150px;width:360px;"></textarea></form>
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue