Skip to content

Commit 373cb57

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-30e3fa72
2 parents f839c74 + 30e3fa7 commit 373cb57

File tree

41 files changed

+169
-112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+169
-112
lines changed

1-js/05-data-types/08-weakmap-weakset/01-recipients-read/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Let's store read messages in `WeakSet`:
22

3-
```js
3+
```js run
44
let messages = [
55
{text: "Hello", from: "John"},
66
{text: "How goes?", from: "John"},

1-js/05-data-types/08-weakmap-weakset/01-recipients-read/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let messages = [
1616

1717
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.
1818

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.
2020

2121
P.S. When a message is removed from `messages`, it should disappear from your structure as well.
2222

1-js/05-data-types/11-date/6-get-seconds-today/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# How many seconds has passed today?
5+
# How many seconds have passed today?
66

77
Write a function `getSecondsToday()` that returns the number of seconds from the beginning of today.
88

9-
For instance, if now `10:00 am`, and there was no daylight savings shift, then:
9+
For instance, if now were `10:00 am`, and there was no daylight savings shift, then:
1010

1111
```js
1212
getSecondsToday() == 36000 // (3600 * 10)

1-js/05-data-types/11-date/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ To create a new `Date` object call `new Date()` with one of the following argume
5858
For instance:
5959

6060
```js
61-
new Date(2011, 0, 1, 0, 0, 0, 0); // // 1 Jan 2011, 00:00:00
61+
new Date(2011, 0, 1, 0, 0, 0, 0); // 1 Jan 2011, 00:00:00
6262
new Date(2011, 0, 1); // the same, hours etc are 0 by default
6363
```
6464

1-js/06-advanced-functions/03-closure/article.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ To summarize:
100100

101101
### Function Declaration
102102

103-
Till now, we only observed variables. Now enter Function Declarations.
103+
Until now, we only observed variables. Now enter Function Declarations.
104104

105105
**Unlike `let` variables, they are fully initialized not when the execution reaches them, but earlier, when a Lexical Environment is created.**
106106

@@ -117,7 +117,7 @@ The code below demonstrates that the Lexical Environment is non-empty from the b
117117

118118
Now let's go on and explore what happens when a function accesses an outer variable.
119119

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.
121121

122122
When a function runs, a new Lexical Environment is created automatically to store local variables and parameters of the call.
123123

@@ -149,7 +149,7 @@ The inner Lexical Environment has a reference to the `outer` one.
149149
150150
**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.**
151151
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.
153153
154154
Let's see how the search proceeds in our example:
155155
@@ -184,8 +184,8 @@ sayHi(); // Pete
184184
The execution flow of the code above:
185185

186186
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"`.
189189

190190

191191
```smart header="One call -- one Lexical Environment"
@@ -313,25 +313,25 @@ Hopefully, the situation with outer variables is clear now. For most situations
313313

314314
## Environments in detail
315315

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.
317317

318318
Please note the additional `[[Environment]]` property is covered here. We didn't mention it before for simplicity.
319319

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:
321321

322322
![](lexenv-nested-makecounter-1.svg)
323323

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.
325325

326326
**All functions "on birth" receive a hidden property `[[Environment]]` with a reference to the Lexical Environment of their creation.**
327327

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.
329329

330330
Here, `makeCounter` is created in the global Lexical Environment, so `[[Environment]]` keeps a reference to it.
331331

332332
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.
333333

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()`:
335335

336336
![](lexenv-nested-makecounter-2.svg)
337337

@@ -392,7 +392,7 @@ A [closure](https://en.wikipedia.org/wiki/Closure_(computer_programming)) is a f
392392
393393
That is: they automatically remember where they were created using a hidden `[[Environment]]` property, and all of them can access outer variables.
394394
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.
396396
```
397397

398398
## Code blocks and loops, IIFE
@@ -469,13 +469,13 @@ The code outside of the block (or inside another script) doesn't see variables i
469469

470470
### IIFE
471471

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.
473473

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).
475475

476476
That's not a thing we should use nowadays, but you can find them in old scripts, so it's better to understand them.
477477

478-
IIFE looks like this:
478+
An IIFE looks like this:
479479

480480
```js run
481481
(function() {
@@ -511,7 +511,7 @@ function go() {
511511
}(); // <-- can't call Function Declaration immediately
512512
```
513513

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.
515515

516516
There exist other ways besides parentheses to tell JavaScript that we mean a Function Expression:
517517

@@ -539,7 +539,7 @@ In all the above cases we declare a Function Expression and run it immediately.
539539

540540
## Garbage collection
541541

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:
543543

544544
```js
545545
function f() {
@@ -550,7 +550,7 @@ function f() {
550550
f();
551551
```
552552

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.
554554

555555
...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:
556556

@@ -584,7 +584,7 @@ let arr = [f(), f(), f()];
584584

585585
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.
586586

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;
588588

589589
```js
590590
function f() {

1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/_js.view/test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ describe("debounce", function() {
77
this.clock.restore();
88
});
99

10+
it("trigger the fuction execution immediately", function () {
11+
let mode;
12+
const f = () => mode='leading';
13+
14+
debounce(f, 1000)(); // runs without a delay
15+
16+
assert.equal(mode, 'leading');
17+
});
18+
1019
it("calls the function at maximum once in ms milliseconds", function() {
1120
let log = '';
1221

@@ -38,4 +47,4 @@ describe("debounce", function() {
3847
obj.f("test");
3948
});
4049

41-
});
50+
});

1-js/06-advanced-functions/09-call-apply-decorators/04-throttle/solution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function throttle(func, ms) {
3333
A call to `throttle(func, ms)` returns `wrapper`.
3434

3535
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.
3838

3939
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.

1-js/06-advanced-functions/09-call-apply-decorators/04-throttle/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Let's check the real-life application to better understand that requirement and
1212

1313
**For instance, we want to track mouse movements.**
1414

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).
1616

1717
**We'd like to update some information on the web-page when the pointer moves.**
1818

@@ -31,8 +31,8 @@ A code example:
3131

3232
```js
3333
function f(a) {
34-
console.log(a)
35-
};
34+
console.log(a);
35+
}
3636

3737
// f1000 passes calls to f at maximum once per 1000 ms
3838
let f1000 = throttle(f, 1000);

1-js/06-advanced-functions/09-call-apply-decorators/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ let worker = {
7575
},
7676

7777
slow(x) {
78-
// actually, there can be a scary CPU-heavy task here
78+
// scary CPU-heavy task here
7979
alert("Called with " + x);
8080
return x * this.someMethod(); // (*)
8181
}
@@ -372,7 +372,7 @@ hash(1, 2);
372372

373373
The trick is called *method borrowing*.
374374

375-
We take (borrow) a join method from a regular array `[].join`. And use `[].join.call` to run it in the context of `arguments`.
375+
We take (borrow) a join method from a regular array (`[].join`) and use `[].join.call` to run it in the context of `arguments`.
376376

377377
Why does it work?
378378

1-js/11-async/01-callbacks/article.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22

33
# Introduction: callbacks
44

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.
67
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`:
818

919
```js
1020
function loadScript(src) {
@@ -14,18 +24,18 @@ function loadScript(src) {
1424
}
1525
```
1626

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.
1828

19-
We can use it like this:
29+
We can use this function like this:
2030

2131
```js
22-
// loads and executes the script
32+
// load and execute the script at the given path
2333
loadScript('/my/script.js');
2434
```
2535

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.
2737

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.
2939

3040
```js
3141
loadScript('/my/script.js');
@@ -34,7 +44,7 @@ loadScript('/my/script.js');
3444
// ...
3545
```
3646

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.
3848

3949
But if we do that immediately after the `loadScript(…)` call, that wouldn't work:
4050

1-js/99-js-misc/01-proxy/01-error-nonexisting/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ function wrap(target) {
1919
user = wrap(user);
2020

2121
alert(user.name); // John
22-
alert(user.age); // Error: Property doesn't exist
22+
alert(user.age); // ReferenceError: Property doesn't exist
2323
```

0 commit comments

Comments
 (0)