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/03-closure/article.md
+8-4Lines changed: 8 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,15 @@
1
1
2
-
# Variable scope
2
+
# Variable scope, closure
3
3
4
-
JavaScript is a very function-oriented language. It gives us a lot of freedom. A function can be created dynamically, passed as an argument to another function and called from a totally different place of code later.
4
+
JavaScript is a very function-oriented language. It gives us a lot of freedom. A function can be created at any moment, passed as an argument to another function, and then called from a totally different place of code later.
5
5
6
-
We already know that a function can access variables outside of it.
6
+
We already know that a function can access variables outside of it ("outer" variables).
7
7
8
-
Now let's expand our knowledge to include more complex scenarios.
8
+
But what happens if outer variables change since a function is created? Will the function get newer values or the old ones?
9
+
10
+
And if a function is passed along as a parameter and called from another place of code, will it get access to outer variables at the new place?
11
+
12
+
Let's expand our knowledge to understand these scenarios and more complex ones.
9
13
10
14
```smart header="We'll talk about `let/const` variables here"
11
15
In JavaScript, there are 3 ways to declare a variable: `let`, `const` (the modern ones), and `var` (the remnant of the past).
Copy file name to clipboardExpand all lines: 1-js/11-async/08-async-await/article.md
+3-4Lines changed: 3 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ f();
67
67
68
68
The function execution "pauses" at the line `(*)` and resumes when the promise settles, with `result` becoming its result. So the code above shows "done!" in one second.
69
69
70
-
Let's emphasize: `await` literally makes JavaScript wait until the promise settles, and then go on with the result. That doesn't cost any CPU resources, because the engine can do other jobs in the meantime: execute other scripts, handle events, etc.
70
+
Let's emphasize: `await` literally suspends the function execution until the promise settles, and then resumes it with the promise result. That doesn't cost any CPU resources, because the JavaScript engine can do other jobs in the meantime: execute other scripts, handle events, etc.
71
71
72
72
It's just a more elegant syntax of getting the promise result than `promise.then`, easier to read and write.
73
73
@@ -83,7 +83,7 @@ function f() {
83
83
}
84
84
```
85
85
86
-
We will get this error if we do not put `async` before a function. As said, `await` only works inside an `async function`.
86
+
We may get this error if we forget to put `async` before a function. As said, `await` only works inside an `async` function.
87
87
````
88
88
89
89
Let's take the `showAvatar()` example from the chapter <info:promise-chaining> and rewrite it using `async/await`:
@@ -139,9 +139,8 @@ But we can wrap it into an anonymous async function, like this:
139
139
...
140
140
})();
141
141
```
142
-
143
-
144
142
````
143
+
145
144
````smart header="`await` accepts \"thenables\""
146
145
Like `promise.then`, `await` allows us to use thenable objects (those with a callable `then` method). The idea is that a third-party object may not be a promise, but promise-compatible: if it supports `.then`, that's enough to use it with `await`.
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/6-pointer-events/article.md
+27-27Lines changed: 27 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,28 @@
1
1
# Pointer events
2
2
3
-
Pointer events is a modern way to handle input from a variety of pointing devices, such as a mouse, a pen/stylus, a touchscreen and so on.
3
+
Pointer events are a modern way to handle input from a variety of pointing devices, such as a mouse, a pen/stylus, a touchscreen, and so on.
4
4
5
5
## The brief history
6
6
7
7
Let's make a small overview, so that you understand the general picture and the place of Pointer Events among other event types.
8
8
9
-
- Long ago, in the past, there existed only mouse events.
9
+
- Long ago, in the past, there were only mouse events.
10
10
11
11
Then touch devices appeared. For the old code to work, they also generate mouse events. For instance, tapping generates `mousedown`. But mouse events were not good enough, as touch devices are more powerful in many aspects. For example, it's possible to touch multiple points at once, and mouse events don't have any properties for that.
12
12
13
13
- So touch events were introduced, such as `touchstart`, `touchend`, `touchmove`, that have touch-specific properties (we don't cover them in detail here, because pointer events are even better).
14
14
15
-
Still, it wasn't enough, as there are many other devices, such as pens, that have their own features. Also, writing a code that listens both touch and mouse events was cumbersome.
15
+
Still, it wasn't enough, as there are many other devices, such as pens, that have their own features. Also, writing code that listens for both touch and mouse events was cumbersome.
16
16
17
17
- To solve these issues, the new standard Pointer Events was introduced. It provides a single set of events for all kinds of pointing devices.
18
18
19
-
As of now, [Pointer Events Level 2](https://www.w3.org/TR/pointerevents2/) specification is supported in all major browsers, while the [Pointer Events Level 3](https://w3c.github.io/pointerevents/) is in the works. Unless you code for Internet Explorer 10 or Safari 12 and below, there's no point in using mouse or touch events any more. We can switch to pointer events.
19
+
As of now, [Pointer Events Level 2](https://www.w3.org/TR/pointerevents2/) specification is supported in all major browsers, while [Pointer Events Level 3](https://w3c.github.io/pointerevents/) is in the works. Unless you code for Internet Explorer 10, or for Safari 12 or below, there's no point in using mouse or touch events any more -- we can switch to pointer events.
20
20
21
-
That said, there are important peculiarities, one should know them to use them correctly and avoid extra surprises. We'll pay attention to them in this article.
21
+
That being said, they have some important peculiarities that one should know in order to use them correctly and avoid surprises. We'll make note of them in this article.
22
22
23
23
## Pointer event types
24
24
25
-
Pointer events are named similar to mouse events:
25
+
Pointer events are named similarly to mouse events:
26
26
27
27
| Pointer Event | Mouse event |
28
28
|---------------|-------------|
@@ -37,30 +37,30 @@ Pointer events are named similar to mouse events:
37
37
|`gotpointercapture`| - |
38
38
|`lostpointercapture`| - |
39
39
40
-
As we can see, for every `mouse<event>`, there's a `pointer<event>` that plays a similar role. Also there are 3 additional pointer events that don't have a corresponding `mouse...` counterpart, we'll soon explain about them.
40
+
As we can see, for every `mouse<event>`, there's a `pointer<event>` that plays a similar role. Also there are 3 additional pointer events that don't have a corresponding `mouse...` counterpart, we'll explain them soon.
41
41
42
42
```smart header="Replacing `mouse<event>` with `pointer<event>` in our code"
43
43
We can replace `mouse<event>` events with `pointer<event>` in our code and expect things to continue working fine with mouse.
44
44
45
-
The support for touch devices will also "magically" improve, but we'll probably need to add `touch-action: none`rule in CSS. See the details below in the section about `pointercancel`.
45
+
The support for touch devices will also "magically" improve, but we'll probably need to add `touch-action: none` in CSS. See the details below in the section about `pointercancel`.
46
46
```
47
47
48
48
## Pointer event properties
49
49
50
-
Pointer events have the same properties as mouse events, such as `clientX/Y`, `target` etc, plus some extra:
50
+
Pointer events have the same properties as mouse events, such as `clientX/Y`, `target`, etc., plus some others:
51
51
52
52
- `pointerId` - the unique identifier of the pointer causing the event.
53
53
54
-
Allows to handle multiple pointers, such as a touchscreen with stylus and multi-touch (explained below).
55
-
- `pointerType` - the pointing device type, must be a string, one of: "mouse", "pen" or "touch".
54
+
Allows us to handle multiple pointers, such as a touchscreen with stylus and multi-touch (explained below).
55
+
- `pointerType` - the pointing device type. Must be a string, one of: "mouse", "pen" or "touch".
56
56
57
57
We can use this property to react differently on various pointer types.
58
58
- `isPrimary` - `true` for the primary pointer (the first finger in multi-touch).
59
59
60
-
For pointers that measure a contact area and pressure, e.g. a finger on the touchscreen, the additional properties can be useful:
60
+
For pointers that measure contact area and pressure, e.g. a finger on the touchscreen, the additional properties can be useful:
61
61
62
-
- `width` - the width of of the area where the pointer touches the device. Where unsupported, e.g. for mouse it's always `1`.
63
-
- `height` - the height of of the area where the pointer touches the device. Where unsupported, always `1`.
62
+
- `width` - the width of the area where the pointer touches the device. Where unsupported, e.g. for a mouse, it's always `1`.
63
+
- `height` - the height of the area where the pointer touches the device. Where unsupported, it's always `1`.
64
64
- `pressure` - the pressure of the pointer tip, in range from 0 to 1. For devices that don't support pressure must be either `0.5` (pressed) or `0`.
65
65
- `tangentialPressure` - the normalized tangential pressure.
66
66
- `tiltX`, `tiltY`, `twist` - pen-specific properties that describe how the pen is positioned relative the surface.
@@ -69,29 +69,29 @@ These properties aren't very well supported across devices, so they are rarely u
69
69
70
70
## Multi-touch
71
71
72
-
One of the things that mouse events totally don't support is multi-touch: a user can touch them in several places at once at their phone or tablet, perform special gestures.
72
+
One of the things that mouse events totally don't support is multi-touch: a user can touch in several places at once on their phone or tablet, or perform special gestures.
73
73
74
-
Pointer Events allow to handle multi-touch with the help of `pointerId` and `isPrimary` properties.
74
+
Pointer Events allow handling multi-touch with the help of the `pointerId` and `isPrimary` properties.
75
75
76
-
Here's what happens when a user touches a screen at one place, and then puts another finger somewhere else on it:
76
+
Here's what happens when a user touches a screen in one place, then puts another finger somewhere else on it:
77
77
78
78
1. At the first touch:
79
79
- `pointerdown` with `isPrimary=true` and some `pointerId`.
80
80
2. For the second finger and further touches:
81
81
- `pointerdown` with `isPrimary=false` and a different `pointerId` for every finger.
82
82
83
-
Please note: there `pointerId` is assigned not to the whole device, but for each touching finger. If we use 5 fingers to simultaneously touch the screen, we have 5 `pointerdown` eventswith respective coordinates and different `pointerId`.
83
+
Please note: the `pointerId` is assigned not to the whole device, but for each touching finger. If we use 5 fingers to simultaneously touch the screen, we have 5 `pointerdown` events, each with their respective coordinates and a different `pointerId`.
84
84
85
85
The events associated with the first finger always have `isPrimary=true`.
86
86
87
-
We can track multiple touching fingers using their `pointerId`. When the user moves move and then detouches a finger, we get `pointermove` and `pointerup` events with the same `pointerId` as we had in `pointerdown`.
87
+
We can track multiple touching fingers using their `pointerId`. When the user moves and then removes a finger, we get `pointermove` and `pointerup` events with the same `pointerId` as we had in `pointerdown`.
88
88
89
89
```online
90
90
Here's the demo that logs `pointerdown` and `pointerup` events:
91
91
92
92
[iframe src="multitouch" edit height=200]
93
93
94
-
Please note: you must be using a touchscreen device, such as a phone or a tablet to actually see the difference. For single-touch devices, such as a mouse, there'll be always same `pointerId` with `isPrimary=true`, for all pointer events.
94
+
Please note: you must be using a touchscreen device, such as a phone or a tablet, to actually see the difference. For single-touch devices, such as a mouse, there'll be always same `pointerId` with `isPrimary=true`, for all pointer events.
95
95
```
96
96
97
97
## Event: pointercancel
@@ -109,7 +109,7 @@ We'll demonstrate `pointercancel` on a practical example to see how it affects u
109
109
110
110
Let's say we're impelementing drag'n'drop for a ball, just as in the beginning of the article <info:mouse-drag-and-drop>.
111
111
112
-
Here are the flow of user actions and corresponding events:
112
+
Here is the flow of user actions and the corresponding events:
113
113
114
114
1) The user presses the mouse button on an image, to start dragging
115
115
-`pointerdown` event fires
@@ -134,13 +134,13 @@ We'd like to implement our own drag'n'drop, so let's tell the browser not to tak
134
134
We need to do two things:
135
135
136
136
1. Prevent native drag'n'drop from happening:
137
-
-Can do it by setting `ball.ondragstart = () => false`, just as described in the article <info:mouse-drag-and-drop>.
137
+
-We can do this by setting `ball.ondragstart = () => false`, just as described in the article <info:mouse-drag-and-drop>.
138
138
- That works well for mouse events.
139
139
2. For touch devices, there are also touch-related browser actions. We'll have problems with them too.
140
140
- We can prevent them by setting `#ball { touch-action: none }` in CSS.
141
141
- Then our code will start working on touch devices.
142
142
143
-
After we do that, the events will work as intended, the browser won't hijack the process and emit no`pointercancel`.
143
+
After we do that, the events will work as intended, the browser won't hijack the process and doesn't emit`pointercancel`.
144
144
145
145
```online
146
146
This demo adds these lines:
@@ -214,16 +214,16 @@ There are two associated pointer events:
214
214
215
215
## Summary
216
216
217
-
Pointer events allow to handle mouse, touch and pen events simultaneously.
217
+
Pointer events allow handling mouse, touch and pen events simultaneously.
218
218
219
219
Pointer events extend mouse events. We can replace `mouse` with `pointer` in event names and expect our code to continue working for mouse, with better support for other device types.
220
220
221
-
Remember to set `touch-events: none` in CSS for elements that we engage, otherwise the browser hijacks many types of touch interactions and pointer events won't be generated.
221
+
Remember to set `touch-events: none` in CSS for elements that we engage, otherwise the browser will hijack many types of touch interactions, and pointer events won't be generated.
222
222
223
223
Additional abilities of Pointer events are:
224
224
225
225
- Multi-touch support using `pointerId` and `isPrimary`.
226
-
- Device-specific properties, such as `pressure`, `width/height` and others.
226
+
- Device-specific properties, such as `pressure`, `width/height`, and others.
227
227
- Pointer capturing: we can retarget all pointer events to a specific element until `pointerup`/`pointercancel`.
228
228
229
-
As of now, pointer events are supported in all major browsers, so we can safely switch to them, if IE10- and Safari 12- are not needed. And even with those browsers, there are polyfills that enable the support of pointer events.
229
+
As of now, pointer events are supported in all major browsers, so we can safely switch to them, as long as IE10- and Safari 12- are not needed. And even with those browsers, there are polyfills that enable the support of pointer events.
Copy file name to clipboardExpand all lines: 5-network/11-websocket/article.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -177,12 +177,12 @@ A call `socket.send(body)` allows `body` in string or a binary format, including
177
177
178
178
**When we receive the data, text always comes as string. And for binary data, we can choose between `Blob` and `ArrayBuffer` formats.**
179
179
180
-
That's set by `socket.bufferType` property, it's `"blob"` by default, so binary data comes as `Blob` objects.
180
+
That's set by `socket.binaryType` property, it's `"blob"` by default, so binary data comes as `Blob` objects.
181
181
182
182
[Blob](info:blob) is a high-level binary object, it directly integrates with `<a>`, `<img>` and other tags, so that's a sane default. But for binary processing, to access individual data bytes, we can change it to `"arraybuffer"`:
183
183
184
184
```js
185
-
socket.bufferType = "arraybuffer";
185
+
socket.binaryType = "arraybuffer";
186
186
socket.onmessage = (event) => {
187
187
// event.data is either a string (if text) or arraybuffer (if binary)
0 commit comments