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

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

Lines changed: 4 additions & 4 deletions
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

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

Lines changed: 1 addition & 3 deletions
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.

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

Lines changed: 1 addition & 1 deletion
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

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

Lines changed: 7 additions & 1 deletion
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

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

Lines changed: 1 addition & 1 deletion
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
};

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

Lines changed: 24 additions & 0 deletions
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+
```

1-js/08-prototypes/03-native-prototypes/article.md

Lines changed: 1 addition & 1 deletion
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.

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

Lines changed: 4 additions & 4 deletions
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

1-js/09-classes/01-class/article.md

Lines changed: 3 additions & 3 deletions
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 {

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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)