Skip to content

Commit c10b445

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-852ee189
2 parents 739ae04 + 852ee18 commit c10b445

44 files changed

Lines changed: 399 additions & 318 deletions

File tree

Some content is hidden

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

1-js/02-first-steps/12-while-for/article.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ for (let i = 0; i < 3; i++) {
309309

310310
let input = prompt(`Value at coords (${i},${j})`, '');
311311

312-
// what if I want to exit from here to Done (below)?
313-
312+
// what if we want to exit from here to Done (below)?
314313
}
315314
}
316315

1-js/04-object-basics/01-object/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ let user = {
154154
};
155155

156156
let key = "name";
157-
user.key // undefined
157+
alert( user.key ) // undefined
158158
```
159159

160160
### Computed properties

1-js/04-object-basics/03-symbol/article.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
By specification, object property keys may be either of string type, or of symbol type. Not numbers, not booleans, only strings or symbols, these two types.
55

6-
Till now we've only seen strings. Now let's see the advantages that symbols can give us.
6+
Till now we've been using only strings. Now let's see the benefits that symbols can give us.
77

88
## Symbols
99

10-
"Symbol" value represents a unique identifier.
10+
A "symbol" represents a unique identifier.
1111

1212
A value of this type can be created using `Symbol()`:
1313

@@ -52,15 +52,15 @@ alert(id); // TypeError: Cannot convert a Symbol value to a string
5252
5353
That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not occasionally convert one into another.
5454
55-
If we really want to show a symbol, we need to call `.toString()` on it, like here:
55+
If we really want to show a symbol, we need to explicitly call `.toString()` on it, like here:
5656
```js run
5757
let id = Symbol("id");
5858
*!*
5959
alert(id.toString()); // Symbol(id), now it works
6060
*/!*
6161
```
6262
63-
Or get `symbol.description` property to get the description only:
63+
Or get `symbol.description` property to show the description only:
6464
```js run
6565
let id = Symbol("id");
6666
*!*
@@ -74,15 +74,19 @@ alert(id.description); // id
7474

7575
Symbols allow us to create "hidden" properties of an object, that no other part of code can occasionally access or overwrite.
7676

77-
For instance, if we're working with `user` objects, that belong to a third-party code and don't have any `id` field. We'd like to add identifiers to them.
77+
For instance, if we're working with `user` objects, that belong to a third-party code. We'd like to add identifiers to them.
7878

7979
Let's use a symbol key for it:
8080

8181
```js run
82-
let user = { name: "John" };
82+
let user = { // belongs to another code
83+
name: "John"
84+
};
85+
8386
let id = Symbol("id");
8487

85-
user[id] = "ID Value";
88+
user[id] = 1;
89+
8690
alert( user[id] ); // we can access the data using the symbol as the key
8791
```
8892

@@ -108,13 +112,13 @@ There will be no conflict between our and their identifiers, because symbols are
108112
```js run
109113
let user = { name: "John" };
110114

111-
// our script uses "id" property
112-
user.id = "ID Value";
115+
// Our script uses "id" property
116+
user.id = "Our id value";
113117

114-
// ...if later another script the uses "id" for its purposes...
118+
// ...Another script also wants "id" for its purposes...
115119

116120
user.id = "Their id value"
117-
// boom! overwritten! it did not mean to harm the colleague, but did it!
121+
// Boom! overwritten by another script!
118122
```
119123

120124
### Symbols in a literal
@@ -129,7 +133,7 @@ let id = Symbol("id");
129133
let user = {
130134
name: "John",
131135
*!*
132-
[id]: 123 // not just "id: 123"
136+
[id]: 123 // not "id: 123"
133137
*/!*
134138
};
135139
```

1-js/05-data-types/05-array-methods/9-shuffle/solution.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ There are other good ways to do the task. For instance, there's a great algorith
6868
function shuffle(array) {
6969
for (let i = array.length - 1; i > 0; i--) {
7070
let j = Math.floor(Math.random() * (i + 1)); // random index from 0 to i
71-
[array[i], array[j]] = [array[j], array[i]]; // swap elements
71+
72+
// swap elements array[i] and array[j]
73+
// we use "destructuring assignment" syntax to achieve that
74+
// you'll find more details about that syntax in later chapters
75+
// same can be written as:
76+
// let t = array[i]; array[i] = array[j]; array[j] = t
77+
[array[i], array[j]] = [array[j], array[i]];
7278
}
7379
}
7480
```

1-js/09-classes/07-mixins/article.md

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

33
In JavaScript we can only inherit from a single object. There can be only one `[[Prototype]]` for an object. And a class may extend only one other class.
44

5-
But sometimes that feels limiting. For instance, I have a class `StreetSweeper` and a class `Bicycle`, and want to make their mix: a `StreetSweepingBicycle`.
5+
But sometimes that feels limiting. For instance, we have a class `StreetSweeper` and a class `Bicycle`, and want to make their mix: a `StreetSweepingBicycle`.
66

77
Or we have a class `User` and a class `EventEmitter` that implements event generation, and we'd like to add the functionality of `EventEmitter` to `User`, so that our users can emit events.
88

99
There's a concept that can help here, called "mixins".
1010

11-
As defined in Wikipedia, a [mixin](https://en.wikipedia.org/wiki/Mixin) is a class that contains methods for use by other classes without having to be the parent class of those other classes.
11+
As defined in Wikipedia, a [mixin](https://en.wikipedia.org/wiki/Mixin) is a class containing methods that can be used by other classes without a need to inherit from it.
1212

1313
In other words, a *mixin* provides methods that implement a certain behavior, but we do not use it alone, we use it to add the behavior to other classes.
1414

1-js/10-error-handling/1-try-catch/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ try {
298298
*!*
299299
alert(e.name); // SyntaxError
300300
*/!*
301-
alert(e.message); // Unexpected token o in JSON at position 0
301+
alert(e.message); // Unexpected token o in JSON at position 2
302302
}
303303
```
304304

1-js/99-js-misc/03-currying-partials/article.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ As you can see, the implementation is straightforward: it's just two wrappers.
4040

4141
- The result of `curry(func)` is a wrapper `function(a)`.
4242
- When it is called like `sum(1)`, the argument is saved in the Lexical Environment, and a new wrapper is returned `function(b)`.
43-
- Then `sum(1)(2)` finally calls `function(b)` providing `2`, and it passes the call to the original multi-argument `sum`.
43+
- Then this wrapper is called with `2` as an argument, and it passes the call to the original `sum`.
4444

4545
More advanced implementations of currying, such as [_.curry](https://lodash.com/docs#curry) from lodash library, return a wrapper that allows a function to be called both normally and partially:
4646

@@ -73,10 +73,15 @@ Let's curry it!
7373
log = _.curry(log);
7474
```
7575

76-
After that `log` work both the normal way and in the curried form:
76+
After that `log` work normally:
77+
78+
```js
79+
log(new Date(), "DEBUG", "some debug"); // log(a, b, c)
80+
```
81+
82+
...But also works in the curried form:
7783

7884
```js
79-
log(new Date(), "DEBUG", "some debug"); // log(a,b,c)
8085
log(new Date())("DEBUG")("some debug"); // log(a)(b)(c)
8186
```
8287

@@ -102,11 +107,11 @@ debugNow("message"); // [HH:mm] DEBUG message
102107

103108
So:
104109
1. We didn't lose anything after currying: `log` is still callable normally.
105-
2. We were able to generate partial functions such as for today's logs.
110+
2. We can easily generate partial functions such as for today's logs.
106111

107112
## Advanced curry implementation
108113

109-
In case you'd like to get in details, here's the "advanced" curry implementation that we could use above.
114+
In case you'd like to get in details, here's the "advanced" curry implementation for multi-argument functions that we could use above.
110115

111116
It's pretty short:
112117

@@ -142,7 +147,7 @@ alert( curriedSum(1)(2)(3) ); // 6, full currying
142147

143148
The new `curry` may look complicated, but it's actually easy to understand.
144149

145-
The result of `curry(func)` is the wrapper `curried` that looks like this:
150+
The result of `curry(func)` call is the wrapper `curried` that looks like this:
146151

147152
```js
148153
// func is the function to transform
@@ -157,7 +162,7 @@ function curried(...args) {
157162
};
158163
```
159164

160-
When we run it, there are two execution branches:
165+
When we run it, there are two `if` execution branches:
161166

162167
1. Call now: if passed `args` count is the same as the original function has in its definition (`func.length`) or longer, then just pass the call to it.
163168
2. Get a partial: otherwise, `func` is not called yet. Instead, another wrapper `pass` is returned, that will re-apply `curried` providing previous arguments together with the new ones. Then on a new call, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
@@ -173,7 +178,9 @@ For the call `curried(1)(2)(3)`:
173178
If that's still not obvious, just trace the calls sequence in your mind or on the paper.
174179

175180
```smart header="Fixed-length functions only"
176-
The currying requires the function to have a known fixed number of arguments.
181+
The currying requires the function to have a fixed number of arguments.
182+
183+
A function that uses rest parameters, such as `f(...args)`, can't be curried this way.
177184
```
178185

179186
```smart header="A little more than currying"

2-ui/3-event-details/1-mouse-events-basics/01-selectable-list/task.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ The demo:
1414
[iframe border="1" src="solution" height=180]
1515

1616
P.S. For this task we can assume that list items are text-only. No nested tags.
17+
1718
P.P.S. Prevent the native browser selection of the text on clicks.

2-ui/3-event-details/1-mouse-events-basics/head.html

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,28 @@
44

55
function showmesg(t, form) {
66

7-
if (timer==0) timer = new Date()
7+
if (timer == 0) {
8+
timer = new Date();
9+
}
10+
11+
let tm = new Date();
812

9-
let tm = new Date()
10-
if (tm-timer > 300) {
11-
t = '------------------------------\n'+t
13+
if (tm - timer > 300) {
14+
t = '------------------------------\n' + t;
1215
}
1316

14-
let area = document.forms[form+'form'].getElementsByTagName('textarea')[0]
17+
let area = document.forms[form + 'form'].getElementsByTagName('textarea')[0];
1518

1619
area.value += t + '\n';
17-
area.scrollTop = area.scrollHeight
20+
area.scrollTop = area.scrollHeight;
1821

19-
timer = tm
22+
timer = tm;
2023
}
2124

2225
function logMouse(e) {
2326
let evt = e.type;
2427
while (evt.length < 11) evt += ' ';
25-
showmesg(evt+" which="+e.which, 'test')
28+
showmesg(evt + " which=" + e.which, 'test')
2629
return false;
2730
}
2831

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/1-behavior-nested-tooltip/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ importance: 5
44

55
# Improved tooltip behavior
66

7-
Write JavaScript that shows a tooltip over an element with the attribute `data-tooltip`.
7+
Write JavaScript that shows a tooltip over an element with the attribute `data-tooltip`. The value of this attribute should become the tooltip text.
88

99
That's like the task <info:task/behavior-tooltip>, but here the annotated elements can be nested. The most deeply nested tooltip is shown.
1010

11+
Only one tooltip may show up at the same time.
12+
1113
For instance:
1214

1315
```html
@@ -21,5 +23,3 @@ For instance:
2123
The result in iframe:
2224

2325
[iframe src="solution" height=300 border=1]
24-
25-
P.S. Hint: only one tooltip may show up at the same time.

0 commit comments

Comments
 (0)