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/04-object-basics/04-object-methods/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
@@ -98,7 +98,7 @@ It's common that an object method needs to access the information stored in the
98
98
99
99
For instance, the code inside `user.sayHi()` may need the name of the `user`.
100
100
101
-
**To access the object, a method can use the `this` keyword.**
101
+
**To access the object, a method can use `this` keyword.**
102
102
103
103
The value of `this` is the object "before dot", the one used to call the method.
104
104
@@ -167,9 +167,9 @@ If we used `this.name` instead of `user.name` inside the `alert`, then the code
167
167
168
168
## "this" is not bound
169
169
170
-
In JavaScript, "this" keyword behaves unlike most other programming languages. It can be used in any function.
170
+
In JavaScript, keyword`this` behaves unlike most other programming languages. It can be used in any function.
171
171
172
-
There's no syntax error in the code like that:
172
+
There's no syntax error in the following example:
173
173
174
174
```js
175
175
functionsayHi() {
@@ -220,13 +220,13 @@ In this case `this` is `undefined` in strict mode. If we try to access `this.nam
220
220
221
221
In non-strict mode the value of `this` in such case will be the *global object* (`window` in a browser, we'll get to it later in the chapter [](info:global-object)). This is a historical behavior that `"use strict"` fixes.
222
222
223
-
Usually such call is an programming error. If there's `this` inside a function, it expects to be called in an object context.
223
+
Usually such call is a programming error. If there's `this` inside a function, it expects to be called in an object context.
224
224
````
225
225
226
226
```smart header="The consequences of unbound `this`"
227
227
If you come from another programming language, then you are probably used to the idea of a "bound `this`", where methods defined in an object always have `this` referencing that object.
228
228
229
-
In JavaScript `this` is "free", its value is evaluated at call-time and does not depend on where the method was declared, but rather on what's the object "before the dot".
229
+
In JavaScript `this` is "free", its value is evaluated at call-time and does not depend on where the method was declared, but rather on whatobject is "before the dot".
230
230
231
231
The concept of run-time evaluated `this` has both pluses and minuses. On the one hand, a function can be reused for different objects. On the other hand, greater flexibility opens a place for mistakes.
Single and double quotes come from ancient times of language creation when the need for multiline strings was not taken into account. Backticks appeared much later and thus are more versatile.
52
52
53
-
Backticks also allow us to specify a "template function" before the first backtick. The syntax is: <code>func`string`</code>. The function `func` is called automatically, receives the string and embedded expressions and can process them. You can read more about it in the [docs](mdn:/JavaScript/Reference/Template_literals#Tagged_template_literals). This is called "tagged templates". This feature makes it easier to wrap strings into custom templating or other functionality, but it is rarely used.
53
+
Backticks also allow us to specify a "template function" before the first backtick. The syntax is: <code>func`string`</code>. The function `func` is called automatically, receives the string and embedded expressions and can process them. You can read more about it in the [docs](mdn:/JavaScript/Reference/Template_literals#Tagged_templates). This is called "tagged templates". This feature makes it easier to wrap strings into custom templating or other functionality, but it is rarely used.
In the call above, we use `user.younger` as a filter and also provide `user` as the context for it. If we didn't provide the context, `users.filter(user.younger)` would call `user.younger` as a standalone function, with `this=undefined`. That would mean an instant error.
686
+
If in the example above we used `users.filter(army.canJoin)`, then `army.canJoin` would be called as a standalone function, with `this=undefined`, thus leading to an instant error.
687
+
688
+
A call to `users.filter(army.canJoin, army)` can be replaced with `users.filter(user => army.canJoin(user))`, that does the same. The former is used more often, as it's a bit easier to understand for most people.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/08-settimeout-setinterval/article.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,8 @@ We may decide to execute a function not right now, but at a certain time later.
4
4
5
5
There are two methods for it:
6
6
7
-
-`setTimeout` allows to run a function once after the interval of time.
8
-
-`setInterval` allows to run a function regularly with the interval between the runs.
7
+
-`setTimeout` allows us to run a function once after the interval of time.
8
+
-`setInterval` allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.
9
9
10
10
These methods are not a part of JavaScript specification. But most environments have the internal scheduler and provide these methods. In particular, they are supported in all browsers and Node.js.
11
11
@@ -239,9 +239,9 @@ There's a side-effect. A function references the outer lexical environment, so,
239
239
240
240
There's a special use case: `setTimeout(func, 0)`, or just `setTimeout(func)`.
241
241
242
-
This schedules the execution of `func` as soon as possible. But scheduler will invoke it only after the current code is complete.
242
+
This schedules the execution of `func` as soon as possible. But the scheduler will invoke it only after the currently executing script is complete.
243
243
244
-
So the function is scheduled to run "right after" the current code.
244
+
So the function is scheduled to run "right after" the current script.
245
245
246
246
For instance, this outputs "Hello", then immediately "World":
The first line "puts the call into calendar after 0ms". But the scheduler will only "check the calendar" after the current code is complete, so `"Hello"` is first, and `"World"` -- after it.
254
+
The first line "puts the call into calendar after 0ms". But the scheduler will only "check the calendar" after the current script is complete, so `"Hello"` is first, and `"World"` -- after it.
255
255
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
@@ -286,10 +286,10 @@ For server-side JavaScript, that limitation does not exist, and there exist othe
286
286
287
287
## Summary
288
288
289
-
- Methods `setInterval(func, delay, ...args)` and `setTimeout(func, delay, ...args)` allow to run the `func`regularly/once after `delay` milliseconds.
290
-
- To cancel the execution, we should call `clearInterval/clearTimeout` with the value returned by `setInterval/setTimeout`.
291
-
- Nested `setTimeout` calls is a more flexible alternative to `setInterval`, allowing to set the time *between* executions more precisely.
292
-
- Zero delay scheduling with `setTimeout(func, 0)` (the same as `setTimeout(func)`) is used to schedule the call "as soon as possible, but after the current code is complete".
289
+
- Methods `setTimeout(func, delay, ...args)` and `setInterval(func, delay, ...args)` allow us to run the `func` once/regularly after `delay` milliseconds.
290
+
- To cancel the execution, we should call `clearTimeout/clearInterval` with the value returned by `setTimeout/setInterval`.
291
+
- Nested `setTimeout` calls is a more flexible alternative to `setInterval`, allowing us to set the time *between* executions more precisely.
292
+
- Zero delay scheduling with `setTimeout(func, 0)` (the same as `setTimeout(func)`) is used to schedule the call "as soon as possible, but after the current script is complete".
293
293
- The browser limits the minimal delay for five or more nested call of `setTimeout` or for `setInterval` (after 5th call) to 4ms. That's for historical reasons.
294
294
295
295
Please note that all scheduling methods do not *guarantee* the exact delay.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/task.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ importance: 5
6
6
7
7
The result of `debounce(f, ms)` decorator should be a wrapper that passes the call to `f` at maximum once per `ms` milliseconds.
8
8
9
-
In other words, when we call a "debounced" function, it guarantees that all other future in the closest `ms` milliseconds will be ignored.
9
+
In other words, when we call a "debounced" function, it guarantees that all future calls to the function made less than `ms` milliseconds after the previous call will be ignored.
setTimeout( () =>f(5), 1500); // ignored (less than 1000 ms from the last run)
22
22
```
23
23
24
-
In practice `debounce` is useful for functions that retrieve/update something when we know that nothing new can be done in such a short period of time, so it's better not to waste resources.
24
+
In practice `debounce` is useful for functions that retrieve/update something when we know that nothing new can be done in such a short period of time, so it's better not to waste resources.
Copy file name to clipboardExpand all lines: 1-js/99-js-misc/01-proxy/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,7 @@ What can we intercept with them?
47
47
48
48
For most operations on objects, there's a so-called "internal method" in JavaScript specificaiton, that describes on the lowest level, how it works. For instance, `[[Get]]` - the internal method to read a property, `[[Set]]` -- the internal method to write a property, and so on. These methods are only used in the specification, we can't call them directly by name.
49
49
50
-
Proxy traps inercept invocations of these methods. They are listed in [Proxy specification](https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots) and in the table below.
50
+
Proxy traps intercept invocations of these methods. They are listed in [Proxy specification](https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots) and in the table below.
51
51
52
52
For every internal method, there's a trap in this table: the name of the method that we can add to `handler` parameter of `new Proxy` to intercept the operation:
53
53
@@ -95,7 +95,7 @@ It triggers when a property is read, with following arguments:
95
95
96
96
-`target` -- is the target object, the one passed as the first argument to `new Proxy`,
97
97
-`property` -- property name,
98
-
-`receiver` -- if the target property is a getter, then `receiver` is the object that's going to be used as `this` in its call. Usually that's the `proxy` object itself (or an object that inherits from it, if we inherit from proxy). Right now we don't need this argument, will be explained in more details letter.
98
+
-`receiver` -- if the target property is a getter, then `receiver` is the object that's going to be used as `this` in its call. Usually that's the `proxy` object itself (or an object that inherits from it, if we inherit from proxy). Right now we don't need this argument, will be explained in more details later.
99
99
100
100
Let's use `get` to implement default values for an object.
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/article.md
+3-5Lines changed: 3 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,9 +67,7 @@ Also move the pointer into the child `div`, and then move it out quickly down th
67
67
```
68
68
69
69
```smart header="If `mouseover` triggered, there must be `mouseout`"
70
-
In case of fast mouse movements, intermediate elements may be ignores, but one thing we know for sure: elements can be only skipped as a whole.
71
-
72
-
If the pointer "officially" entered an element with `mouseover`, then upon leaving it we always get `mouseout`.
70
+
In case of fast mouse movements, intermediate elements may be ignored, but one thing we know for sure: if the pointer "officially" entered an element with `mouseover`, then upon leaving it we always get `mouseout`.
If the code inside the handlers doesn't look at `target`, then it might think that the mouse left the`parent` element, and then came back over it. But it's not the case! The mouse never left, it just moved to the child element.
112
+
If we don't examine `event.target` inside the handlers, then it may seem that the mouse pointer left`parent` element, and then came back over it. But it's not the case! The mouse never left, it just moved to the child element.
115
113
116
114
If there's some action upon leaving the element, e.g. animation runs, then such interpretation may bring unwanted side effects.
117
115
@@ -206,4 +204,4 @@ These things are good to note:
206
204
207
205
Events `mouseover/out` trigger even when we go from the parent element to a child element. The browser assumes that the mouse can be only over one element at one time -- the deepest one.
208
206
209
-
Events `mouseenter/leave` are different in that aspect: they only trigger when the mouse comes in and out the element as a whole. Also they do not bubble.
207
+
Events `mouseenter/leave` are different in that aspect: they only trigger when the mouse comes in and out the element as a whole. Also they do not bubble.
0 commit comments