Skip to content

Commit bf46b31

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-71ff8f81
2 parents 05475a3 + 71ff8f8 commit bf46b31

File tree

21 files changed

+106
-76
lines changed

21 files changed

+106
-76
lines changed

Diff for: 1-js/04-object-basics/02-garbage-collection/article.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ The following "garbage collection" steps are regularly performed:
156156
- The garbage collector takes roots and "marks" (remembers) them.
157157
- Then it visits and "marks" all references from them.
158158
- Then it visits marked objects and marks *their* references. All visited objects are remembered, so as not to visit the same object twice in the future.
159-
- ...And so on until there are unvisited references (reachable from the roots).
159+
- ...And so on until every reachable (from the roots) references are visited.
160160
- All objects except marked ones are removed.
161161

162162
For instance, let our object structure look like this:
@@ -181,17 +181,17 @@ Now the objects that could not be visited in the process are considered unreacha
181181

182182
![](garbage-collection-5.svg)
183183

184-
That's the concept of how garbage collection works.
184+
We can also imagine the process as spilling a huge bucket of paint from the roots, that flows through all references and marks all reachable objects. The unmarked ones are then removed.
185185

186-
JavaScript engines apply many optimizations to make it run faster and not affect the execution.
186+
That's the concept of how garbage collection works. JavaScript engines apply many optimizations to make it run faster and not affect the execution.
187187

188188
Some of the optimizations:
189189

190190
- **Generational collection** -- objects are split into two sets: "new ones" and "old ones". Many objects appear, do their job and die fast, they can be cleaned up aggressively. Those that survive for long enough, become "old" and are examined less often.
191191
- **Incremental collection** -- if there are many objects, and we try to walk and mark the whole object set at once, it may take some time and introduce visible delays in the execution. So the engine tries to split the garbage collection into pieces. Then the pieces are executed one by one, separately. That requires some extra bookkeeping between them to track changes, but we have many tiny delays instead of a big one.
192192
- **Idle-time collection** -- the garbage collector tries to run only while the CPU is idle, to reduce the possible effect on the execution.
193193

194-
There are other optimizations and flavours of garbage collection algorithms. As much as I'd like to describe them here, I have to hold off, because different engines implement different tweaks and techniques. And, what's even more important, things change as engines develop, so going deeper "in advance", without a real need is probably not worth that. Unless, of course, it is a matter of pure interest, then there will be some links for you below.
194+
There exist other optimizations and flavours of garbage collection algorithms. As much as I'd like to describe them here, I have to hold off, because different engines implement different tweaks and techniques. And, what's even more important, things change as engines develop, so studying deeper "in advance", without a real need is probably not worth that. Unless, of course, it is a matter of pure interest, then there will be some links for you below.
195195

196196
## Summary
197197

Diff for: 1-js/05-data-types/09-keys-values-entries/article.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,4 @@ let doublePrices = Object.fromEntries(
9999
alert(doublePrices.meat); // 8
100100
```
101101

102-
It may look difficult from the first sight, but becomes easy to understand after you use it once or twice.
103-
104-
We can make powerful one-liners for more complex transforms this way. It's only important to keep balance, so that the code is still simple enough to understand it.
102+
It may look difficult from the first sight, but becomes easy to understand after you use it once or twice. We can make powerful chains of transforms this way.

Diff for: 1-js/05-data-types/10-destructuring-assignment/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ The problem is that JavaScript treats `{...}` in the main code flow (not inside
356356
}
357357
```
358358

359-
So here JavaScript assumes that we have a code block, that's why there's an error. We have destructuring instead.
359+
So here JavaScript assumes that we have a code block, that's why there's an error. We want destructuring instead.
360360

361361
To show JavaScript that it's not a code block, we can wrap the expression in parentheses `(...)`:
362362

Diff for: 1-js/06-advanced-functions/01-recursion/article.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,13 @@ Here's the context stack when we entered the subcall `pow(2, 2)`:
185185
186186
The new current execution context is on top (and bold), and previous remembered contexts are below.
187187
188-
When we finish the subcall -- it is easy to resume the previous context, because it keeps both variables and the exact place of the code where it stopped. Here in the picture we use the word "line", but of course it's more precise.
188+
When we finish the subcall -- it is easy to resume the previous context, because it keeps both variables and the exact place of the code where it stopped.
189+
190+
```smart
191+
Here in the picture we use the word "line", as our example there's only one subcall in line, but generally a single line of code may contain multiple subcalls, like `pow(…) + pow(…) + somethingElse(…)`.
192+
193+
So it would be more precise to say that the execution resumes "immediately after the subcall".
194+
```
189195
190196
### pow(2, 1)
191197

Diff for: 1-js/08-prototypes/01-prototype-inheritance/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ If we look for a property in `rabbit`, and it's missing, JavaScript automaticall
4343
4444
For instance:
4545
46-
```js run
46+
```js
4747
let animal = {
4848
eats: true
4949
};

Diff for: 1-js/08-prototypes/03-native-prototypes/2-defer-to-prototype-extended/solution.md

+24
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,27 @@ function f(a, b) {
1515

1616
f.defer(1000)(1, 2); // shows 3 after 1 sec
1717
```
18+
19+
Please note: we use `this` in `f.apply` to make our decoration work for object methods.
20+
21+
So if the wrapper function is called as an object method, then `this` is passed to the original method `f`.
22+
23+
```js run
24+
Function.prototype.defer = function(ms) {
25+
let f = this;
26+
return function(...args) {
27+
setTimeout(() => f.apply(this, args), ms);
28+
}
29+
};
30+
31+
let user = {
32+
name: "John",
33+
sayHi() {
34+
alert(this.name);
35+
}
36+
}
37+
38+
user.sayHi = user.sayHi.defer(1000);
39+
40+
user.sayHi();
41+
```

Diff for: 1-js/08-prototypes/03-native-prototypes/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,4 @@ Borrowing methods is flexible, it allows to mix functionality from different obj
193193
- The methods are stored in the prototype (`Array.prototype`, `Object.prototype`, `Date.prototype` etc).
194194
- The object itself stores only the data (array items, object properties, the date).
195195
- Primitives also store methods in prototypes of wrapper objects: `Number.prototype`, `String.prototype`, `Boolean.prototype`. Only `undefined` and `null` do not have wrapper objects.
196-
- Built-in prototypes can be modified or populated with new methods. But it's not recommended to change them. Probably the only allowable cause is when we add-in a new standard, but not yet supported by the engine JavaScript method.
196+
- Built-in prototypes can be modified or populated with new methods. But it's not recommended to change them. Probably the only allowable case is when we add-in a new standard, but not yet supported by the engine JavaScript method.

Diff for: 1-js/08-prototypes/04-prototype-methods/article.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let rabbit = Object.create(animal);
2828
alert(rabbit.eats); // true
2929

3030
*!*
31-
alert(Object.getPrototypeOf(rabbit) === animal); // get the prototype of rabbit
31+
alert(Object.getPrototypeOf(rabbit) === animal); // true
3232
*/!*
3333

3434
*!*
@@ -71,9 +71,9 @@ Why so?
7171

7272
That's for historical reasons.
7373

74-
- The `"prototype"` property of a constructor function works since very ancient times.
75-
- Later, in the year 2012: `Object.create` appeared in the standard. It gave the ability to create objects with a given prototype, but did not provide the ability to get/set it. So browsers implemented the non-standard `__proto__` accessor that allowed the user to get/set a prototype at any time.
76-
- Later, in the year 2015: `Object.setPrototypeOf` and `Object.getPrototypeOf` were added to the standard, to perform the same functionality as `__proto__`. As `__proto__` was de-facto implemented everywhere, it was kind-of deprecated and made its way to the Annex B of the standard, that is: optional for non-browser environments.
74+
- The `"prototype"` property of a constructor function has worked since very ancient times.
75+
- Later, in the year 2012, `Object.create` appeared in the standard. It gave the ability to create objects with a given prototype, but did not provide the ability to get/set it. So browsers implemented the non-standard `__proto__` accessor that allowed the user to get/set a prototype at any time.
76+
- Later, in the year 2015, `Object.setPrototypeOf` and `Object.getPrototypeOf` were added to the standard, to perform the same functionality as `__proto__`. As `__proto__` was de-facto implemented everywhere, it was kind-of deprecated and made its way to the Annex B of the standard, that is: optional for non-browser environments.
7777

7878
As of now we have all these ways at our disposal.
7979

Diff for: 1-js/09-classes/01-class/article.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ What `class User {...}` construct really does is:
8989
1. Creates a function named `User`, that becomes the result of the class declaration. The function code is taken from the `constructor` method (assumed empty if we don't write such method).
9090
2. Stores class methods, such as `sayHi`, in `User.prototype`.
9191

92-
Afterwards, for `new User` objects, when we call a method, it's taken from the prototype, just as described in the chapter <info:function-prototype>. So the object has access to class methods.
92+
After `new User` object is created, when we call its method, it's taken from the prototype, just as described in the chapter <info:function-prototype>. So the object has access to class methods.
9393

9494
We can illustrate the result of `class User` declaration as:
9595

@@ -142,11 +142,11 @@ user.sayHi();
142142

143143
The result of this definition is about the same. So, there are indeed reasons why `class` can be considered a syntax sugar to define a constructor together with its prototype methods.
144144

145-
Although, there are important differences.
145+
Still, there are important differences.
146146

147147
1. First, a function created by `class` is labelled by a special internal property `[[FunctionKind]]:"classConstructor"`. So it's not entirely the same as creating it manually.
148148

149-
Unlike a regular function, a class constructor must be called with `new`:
149+
And unlike a regular function, a class constructor must be called with `new`:
150150

151151
```js run
152152
class User {

Diff for: 1-js/09-classes/02-class-inheritance/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class Rabbit extends Animal {
131131
}
132132
```
133133
134-
...But usually we don't want to totally replace a parent method, but rather to build on top of it, tweak or extend its functionality. We do something in our method, but call the parent method before/after it or in the process.
134+
...But usually we don't want to totally replace a parent method, but rather to build on top of it to tweak or extend its functionality. We do something in our method, but call the parent method before/after it or in the process.
135135
136136
Classes provide `"super"` keyword for that.
137137

Diff for: 1-js/09-classes/03-static-properties-methods/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class Rabbit extends Animal {}
192192
alert(Rabbit.__proto__ === Animal); // true
193193

194194
// for regular methods
195-
alert(Rabbit.prototype.__proto__ === Animal.prototype);
195+
alert(Rabbit.prototype.__proto__ === Animal.prototype); // true
196196
```
197197

198198
## Summary

Diff for: 1-js/09-classes/05-extend-natives/article.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ alert(filteredArr); // 10, 50
2121
alert(filteredArr.isEmpty()); // false
2222
```
2323

24-
Please note a very interesting thing. Built-in methods like `filter`, `map` and others -- return new objects of exactly the inherited type `PowerArray`. Their internal implementation uses object `constructor` property for that.
24+
Please note a very interesting thing. Built-in methods like `filter`, `map` and others -- return new objects of exactly the inherited type `PowerArray`. Their internal implementation uses the object's `constructor` property for that.
2525

2626
In the example above,
2727
```js
@@ -74,7 +74,7 @@ Built-in objects have their own static methods, for instance `Object.keys`, `Arr
7474

7575
As we already know, native classes extend each other. For instance, `Array` extends `Object`.
7676

77-
Normally, when one class extends another, both static and non-static methods are inherited. That was thoroughly explained in the chapter [](info:static-properties-methods#statics-and-inheritance).
77+
Normally, when one class extends another, both static and non-static methods are inherited. That was thoroughly explained in the article [](info:static-properties-methods#statics-and-inheritance).
7878

7979
But built-in classes are an exception. They don't inherit statics from each other.
8080

Diff for: 1-js/12-generators-iterators/1-generators/article.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ function* generateSequence(start, end) {
239239

240240
Now we'd like to reuse it for generation of a more complex sequence:
241241
- first, digits `0..9` (with character codes 48..57),
242-
- followed by alphabet letters `A..Z` (character codes 65..90)
243-
- followed by uppercased letters `a..z` (character codes 97..122)
242+
- followed by uppercase alphabet letters `A..Z` (character codes 65..90)
243+
- followed by lowercase alphabet letters `a..z` (character codes 97..122)
244244

245245
We can use this sequence e.g. to create passwords by selecting characters from it (could add syntax characters as well), but let's generate it first.
246246

Diff for: 1-js/12-generators-iterators/2-async-iterators-generators/article.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ let range = {
7272
current: this.from,
7373
last: this.to,
7474

75-
// next() is called on each iteration by the for..of loop
75+
// next() is called on each iteration by the for await..of loop
7676
*!*
7777
async next() { // (2)
7878
// it should return the value as an object {done:.., value :...}
@@ -183,7 +183,7 @@ Now we have the async generator, iterable with `for await...of`.
183183
184184
It's indeed very simple. We add the `async` keyword, and the generator now can use `await` inside of it, rely on promises and other async functions.
185185
186-
Technically, another the difference of an async generator is that its `generator.next()` method is now asynchronous also, it returns promises.
186+
Technically, another difference of an async generator is that its `generator.next()` method is now asynchronous also, it returns promises.
187187
188188
In a regular generator we'd use `result = generator.next()` to get values. In an async generator, we should add `await`, like this:
189189
@@ -265,15 +265,15 @@ Now values come with a delay of 1 second between them.
265265
266266
So far we've seen simple examples, to gain basic understanding. Now let's review a real-life use case.
267267
268-
There are many online services that deliver paginated data. For instance, when we need a list of users, a request returns a pre-defined count (e.g. 100 users) - "one page", and provides an URL to the next page.
268+
There are many online services that deliver paginated data. For instance, when we need a list of users, a request returns a pre-defined count (e.g. 100 users) - "one page", and provides a URL to the next page.
269269
270270
The pattern is very common, it's not about users, but just about anything. For instance, GitHub allows to retrieve commits in the same, paginated fashion:
271271
272272
- We should make a request to URL in the form `https://api.github.com/repos/<repo>/commits`.
273273
- It responds with a JSON of 30 commits, and also provides a link to the next page in the `Link` header.
274274
- Then we can use that link for the next request, to get more commits, and so on.
275275
276-
But we'd like to have is a simpler API: an iterable object with commits, so that we could go over them like this:
276+
But we'd like to have a simpler API: an iterable object with commits, so that we could go over them like this:
277277
278278
```js
279279
let repo = 'javascript-tutorial/en.javascript.info'; // GitHub repository to get commits from

Diff for: 2-ui/1-document/01-browser-environment/article.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Browser environment, specs
22

3-
The JavaScript language was initially created for web browsers. Since then, it has evolved and become a language with many uses and platforms.
3+
The JavaScript language was initially created for web browsers. Since then it has evolved and become a language with many uses and platforms.
44

55
A platform may be a browser, or a web-server or another *host*, even a coffee machine. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*.
66

@@ -60,14 +60,14 @@ For instance, server-side scripts that download HTML pages and process them can
6060
```
6161

6262
```smart header="CSSOM for styling"
63-
CSS rules and stylesheets are structured in a different way than HTML. There's a separate specification [CSSOM](https://www.w3.org/TR/cssom-1/) that explains how they are represented as objects, and how to read and write them.
63+
CSS rules and stylesheets are structured in a different way than HTML. There's a separate specification, [CSS Object Model (CSSOM)](https://www.w3.org/TR/cssom-1/), that explains how they are represented as objects, and how to read and write them.
6464
6565
CSSOM is used together with DOM when we modify style rules for the document. In practice though, CSSOM is rarely required, because usually CSS rules are static. We rarely need to add/remove CSS rules from JavaScript, but that's also possible.
6666
```
6767

68-
## BOM (Browser object model)
68+
## BOM (Browser Object Model)
6969

70-
Browser Object Model (BOM) are additional objects provided by the browser (host environment) to work with everything except the document.
70+
The Browser Object Model (BOM) represents additional objects provided by the browser (host environment) for working with everything except the document.
7171

7272
For instance:
7373

0 commit comments

Comments
 (0)