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/05-data-types/08-weakmap-weakset/01-recipients-read/task.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ let messages = [
16
16
17
17
Your code can access it, but the messages are managed by someone else's code. New messages are added, old ones are removed regularly by that code, and you don't know the exact moments when it happens.
18
18
19
-
Now, which data structure you could use to store information whether the message "have been read"? The structure must be well-suited to give the answer "was it read?" for the given message object.
19
+
Now, which data structure could you use to store information about whether the message "has been read"? The structure must be well-suited to give the answer "was it read?" for the given message object.
20
20
21
21
P.S. When a message is removed from `messages`, it should disappear from your structure as well.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/03-closure/article.md
+18-18Lines changed: 18 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,7 @@ To summarize:
100
100
101
101
### Function Declaration
102
102
103
-
Till now, we only observed variables. Now enter Function Declarations.
103
+
Until now, we only observed variables. Now enter Function Declarations.
104
104
105
105
**Unlike `let` variables, they are fully initialized not when the execution reaches them, but earlier, when a Lexical Environment is created.**
106
106
@@ -117,7 +117,7 @@ The code below demonstrates that the Lexical Environment is non-empty from the b
117
117
118
118
Now let's go on and explore what happens when a function accesses an outer variable.
119
119
120
-
During the call, `say()` uses the outer variable `phrase`, let's look at the details of what's going on.
120
+
During the call, `say()` uses the outer variable `phrase`. Let's look at the details of what's going on.
121
121
122
122
When a function runs, a new Lexical Environment is created automatically to store local variables and parameters of the call.
123
123
@@ -149,7 +149,7 @@ The inner Lexical Environment has a reference to the `outer` one.
149
149
150
150
**When the code wants to access a variable -- the inner Lexical Environment is searched first, then the outer one, then the more outer one and so on until the global one.**
151
151
152
-
If a variable is not found anywhere, that's an error in strict mode (without`use strict`, an assignment to a non-existing variable, like `user ="John"` creates a new global variable `user`, that's for backwards compatibility).
152
+
If a variable is not found anywhere, that's an error in strict mode. Without`use strict`, an assignment to a non-existing variable like `user ="John"` creates a new global variable `user`. That's for backwards compatibility.
153
153
154
154
Let's see how the search proceeds in our example:
155
155
@@ -184,8 +184,8 @@ sayHi(); // Pete
184
184
The execution flow of the code above:
185
185
186
186
1. The global Lexical Environment has `name: "John"`.
187
-
2. At the line `(*)` the global variable is changed, now it has `name: "Pete"`.
188
-
3. When the function `sayHi()`, is executed and takes `name` from outside. Here that's from the global Lexical Environment where it's already `"Pete"`.
187
+
2. At the line `(*)` the global variable is changed. Now it has `name: "Pete"`.
188
+
3. When the function `sayHi()` is executed it takes `name` from outside, the global Lexical Environment, where its value is already `"Pete"`.
189
189
190
190
191
191
```smart header="One call -- one Lexical Environment"
@@ -313,25 +313,25 @@ Hopefully, the situation with outer variables is clear now. For most situations
313
313
314
314
## Environments in detail
315
315
316
-
Here's what's going on in the `makeCounter` example step-by-step, follow it to make sure that you understand how it works in detail.
316
+
Here's what's going on in the `makeCounter` example step-by-step. Follow it to make sure that you understand how it works in detail.
317
317
318
318
Please note the additional `[[Environment]]` property is covered here. We didn't mention it before for simplicity.
319
319
320
-
1. When the script has just started, there is only global Lexical Environment:
320
+
1. When the script has just started, there is only the global Lexical Environment:
321
321
322
322

323
323
324
-
At that starting moment there is only `makeCounter` function, because it's a Function Declaration. It did not run yet.
324
+
At that starting moment there is only the `makeCounter` function, because it's a Function Declaration. It did not run yet.
325
325
326
326
**All functions "on birth" receive a hidden property `[[Environment]]` with a reference to the Lexical Environment of their creation.**
327
327
328
-
We didn't talk about it yet, that's how the function knows where it was made.
328
+
We didn't talk about it before. That's how the function knows where it was made.
329
329
330
330
Here, `makeCounter` is created in the global Lexical Environment, so `[[Environment]]` keeps a reference to it.
331
331
332
332
In other words, a function is "imprinted" with a reference to the Lexical Environment where it was born. And `[[Environment]]` is the hidden function property that has that reference.
333
333
334
-
2. The code runs on, the new global variable `counter` is declared and gets the result of `makeCounter()` call. Here's a snapshot of the moment when the execution is on the first line inside `makeCounter()`:
334
+
2. The code runs on, the new global variable `counter` is declared and gets the result of the `makeCounter()` call. Here's a snapshot of the moment when the execution is on the first line inside `makeCounter()`:
335
335
336
336

337
337
@@ -392,7 +392,7 @@ A [closure](https://en.wikipedia.org/wiki/Closure_(computer_programming)) is a f
392
392
393
393
That is: they automatically remember where they were created using a hidden `[[Environment]]` property, and all of them can access outer variables.
394
394
395
-
When on an interview, a frontend developer gets a question about "what's a closure?", a valid answer would be a definition of the closure and an explanation that all functions in JavaScript are closures, and maybe few more words about technical details: the `[[Environment]]` property and how Lexical Environments work.
395
+
When on an interview, a frontend developer gets a question about "what's a closure?", a valid answer would be a definition of the closure and an explanation that all functions in JavaScript are closures, and maybe a few more words about technical details: the `[[Environment]]` property and how Lexical Environments work.
396
396
```
397
397
398
398
## Code blocks and loops, IIFE
@@ -469,13 +469,13 @@ The code outside of the block (or inside another script) doesn't see variables i
469
469
470
470
### IIFE
471
471
472
-
In the past, there were no block-level lexical environment in JavaScript.
472
+
In the past, there were no block-level lexical environments in JavaScript.
473
473
474
-
So programmers had to invent something. And what they did is called "immediately-invoked function expressions" (abbreviated as IIFE).
474
+
So programmers had to invent something. And what they did was called "immediately-invoked function expressions" (abbreviated as IIFE).
475
475
476
476
That's not a thing we should use nowadays, but you can find them in old scripts, so it's better to understand them.
477
477
478
-
IIFE looks like this:
478
+
An IIFE looks like this:
479
479
480
480
```js run
481
481
(function() {
@@ -511,7 +511,7 @@ function go() {
511
511
}(); // <-- can't call Function Declaration immediately
512
512
```
513
513
514
-
So, parentheses around the function is a trick to show JavaScript that the function is created in the context of another expression, and hence it's a Function Expression: it needs no name and can be called immediately.
514
+
So, the parentheses around the function is a trick to show JavaScript that the function is created in the context of another expression, and hence it's a Function Expression: it needs no name and can be called immediately.
515
515
516
516
There exist other ways besides parentheses to tell JavaScript that we mean a Function Expression:
517
517
@@ -539,7 +539,7 @@ In all the above cases we declare a Function Expression and run it immediately.
539
539
540
540
## Garbage collection
541
541
542
-
Usually, a Lexical Environment is cleaned up and deleted after the function run. For instance:
542
+
Usually, a Lexical Environment is cleaned up and deleted after the function runs. For instance:
543
543
544
544
```js
545
545
functionf() {
@@ -550,7 +550,7 @@ function f() {
550
550
f();
551
551
```
552
552
553
-
Here two values are technically the properties of the Lexical Environment. But after `f()` finishes that Lexical Environment becomes unreachable, so it's deleted from the memory.
553
+
Here, two values are technically the properties of the Lexical Environment. But after `f()` finishes, that Lexical Environment becomes unreachable, so it's deleted from the memory.
554
554
555
555
...But if there's a nested function that is still reachable after the end of `f`, then it has `[[Environment]]` property that references the outer lexical environment, so it's also reachable and alive:
556
556
@@ -584,7 +584,7 @@ let arr = [f(), f(), f()];
584
584
585
585
A Lexical Environment object dies when it becomes unreachable (just like any other object). In other words, it exists only while there's at least one nested function referencing it.
586
586
587
-
In the code below, after `g` becomes unreachable, enclosing Lexical Environment (and hence the `value`) is cleaned from memory;
587
+
In the code below, after `g` becomes unreachable, its enclosing Lexical Environment (and hence the `value`) is cleaned from memory;
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/09-call-apply-decorators/04-throttle/solution.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ function throttle(func, ms) {
33
33
A call to `throttle(func, ms)` returns `wrapper`.
34
34
35
35
1. During the first call, the `wrapper` just runs `func` and sets the cooldown state (`isThrottled = true`).
36
-
2. In this state all calls memorized in `savedArgs/savedThis`. Please note that both the context and the arguments are equally important and should be memorized. We need them simultaneously to reproduce the call.
37
-
3....Then after `ms` milliseconds pass, `setTimeout` triggers. The cooldown state is removed (`isThrottled = false`). And if we had ignored calls, then `wrapper` is executed with last memorized arguments and context.
36
+
2. In this state all calls are memorized in `savedArgs/savedThis`. Please note that both the context and the arguments are equally important and should be memorized. We need them simultaneously to reproduce the call.
37
+
3.After `ms` milliseconds pass, `setTimeout` triggers. The cooldown state is removed (`isThrottled = false`) and, if we had ignored calls, `wrapper` is executed with the last memorized arguments and context.
38
38
39
39
The 3rd step runs not `func`, but `wrapper`, because we not only need to execute `func`, but once again enter the cooldown state and setup the timeout to reset it.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/09-call-apply-decorators/04-throttle/task.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ Let's check the real-life application to better understand that requirement and
12
12
13
13
**For instance, we want to track mouse movements.**
14
14
15
-
In browser we can setup a function to run at every mouse movement and get the pointer location as it moves. During an active mouse usage, this function usually runs very frequently, can be something like 100 times per second (every 10 ms).
15
+
In a browser we can setup a function to run at every mouse movement and get the pointer location as it moves. During an active mouse usage, this function usually runs very frequently, can be something like 100 times per second (every 10 ms).
16
16
17
17
**We'd like to update some information on the web-page when the pointer moves.**
18
18
@@ -31,8 +31,8 @@ A code example:
31
31
32
32
```js
33
33
functionf(a) {
34
-
console.log(a)
35
-
};
34
+
console.log(a);
35
+
}
36
36
37
37
// f1000 passes calls to f at maximum once per 1000 ms
Copy file name to clipboardExpand all lines: 1-js/11-async/01-callbacks/article.md
+18-8Lines changed: 18 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,19 @@
2
2
3
3
# Introduction: callbacks
4
4
5
-
Many actions in JavaScript are *asynchronous*.
5
+
```warn header="We use browser methods here"
6
+
To demonstrate the use of callbacks, promises and other abstract concepts, we'll be using some browser methods; specifically, loading scripts and performing simple document manipulations.
6
7
7
-
For instance, take a look at the function `loadScript(src)`:
8
+
If you're not familiar with these methods, and their usage in the examples is confusing, or if you would just like to understand them better, you may want to read a few chapters from the [next part](/document) of the tutorial.
9
+
```
10
+
11
+
Many actions in JavaScript are *asynchronous*. In other words, we initiate them now, but they finish later.
12
+
13
+
For instance, we can schedule such actions using `setTimeout`.
14
+
15
+
There are other real-world examples of asynchronous actions, e.g. loading scripts and modules (we'll cover them in later chapters).
16
+
17
+
Take a look at the function `loadScript(src)`, that loads a script with the given `src`:
8
18
9
19
```js
10
20
functionloadScript(src) {
@@ -14,18 +24,18 @@ function loadScript(src) {
14
24
}
15
25
```
16
26
17
-
The purpose of the function is to load a new script. When it adds the `<script src="…">` to the document, the browser loads and executes it.
27
+
It appends to the document the new, dynamically created, tag `<script src="…">`, the browser loads and executes it.
18
28
19
-
We can use it like this:
29
+
We can use this function like this:
20
30
21
31
```js
22
-
//loads and executes the script
32
+
//load and execute the script at the given path
23
33
loadScript('/my/script.js');
24
34
```
25
35
26
-
The function is called "asynchronously," because the action (script loading) finishes not now, but later.
36
+
The script is executed "asynchronously", as it starts loading starts now, but runs later, when the function has already finished.
27
37
28
-
If there's a code below `loadScript(…)`, it doesn't wait until the loading finishes.
38
+
If there's a code below `loadScript(…)`, it doesn't wait until the script loading finishes.
29
39
30
40
```js
31
41
loadScript('/my/script.js');
@@ -34,7 +44,7 @@ loadScript('/my/script.js');
34
44
// ...
35
45
```
36
46
37
-
We'd like to use the new script as soon as it loads. It declares new functions, and we want to run them.
47
+
Let's say we need to use the new script as soon as it loads. It declares new functions, and we want to run them.
38
48
39
49
But if we do that immediately after the `loadScript(…)` call, that wouldn't work:
0 commit comments