Skip to content

Commit 2c00b56

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-468e3552
2 parents 66a24e9 + 468e355 commit 2c00b56

File tree

7 files changed

+65
-46
lines changed

7 files changed

+65
-46
lines changed

1-js/02-first-steps/08-operators/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ alert( x ); // 5
219219
220220
The fact of `=` being an operator, not a "magical" language construct has an interesting implication.
221221
222-
Most operators in JavaScript return a value. That's obvious for `+` and `-`, but also true for `=`.
222+
All operators in JavaScript return a value. That's obvious for `+` and `-`, but also true for `=`.
223223

224224
The call `x = value` writes the `value` into `x` *and then returns it*.
225225

1-js/02-first-steps/18-javascript-specials/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ We covered three ways to create a function in JavaScript:
273273
```
274274
275275
276-
- Functions may have local variables: those declared inside its body. Such variables are only visible inside the function.
276+
- Functions may have local variables: those declared inside its body or its parameter list. Such variables are only visible inside the function.
277277
- Parameters can have default values: `function sum(a = 1, b = 2) {...}`.
278278
- Functions always return something. If there's no `return` statement, then the result is `undefined`.
279279

1-js/03-code-quality/01-debugging-chrome/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ There are buttons for it at the top of the right panel. Let's engage them.
135135
Clicking this again and again will step through all script statements one by one.
136136

137137
<span class="devtools" style="background-position:-62px -192px"></span> -- "Step over": run the next command, but *don't go into a function*, hotkey `key:F10`.
138-
: Similar to the previous the "Step" command, but behaves differently if the next statement is a function call. That is: not a built-in, like `alert`, but a function of our own.
138+
: Similar to the previous "Step" command, but behaves differently if the next statement is a function call. That is: not a built-in, like `alert`, but a function of our own.
139139

140140
The "Step" command goes into it and pauses the execution at its first line, while "Step over" executes the nested function call invisibly, skipping the function internals.
141141

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

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

33
The two most used data structures in JavaScript are `Object` and `Array`.
44

5-
Objects allow us to create a single entity that stores data items by key, and arrays allow us to gather data items into an ordered collection.
5+
- Objects allow us to create a single entity that stores data items by key.
6+
- Arrays allow us to gather data items into an ordered list.
67

7-
But when we pass those to a function, it may need not an object/array as a whole, but rather individual pieces.
8+
Although, when we pass those to a function, it may need not an object/array as a whole. It may need individual pieces.
89

9-
*Destructuring assignment* is a special syntax that allows us to "unpack" arrays or objects into a bunch of variables, as sometimes that's more convenient. Destructuring also works great with complex functions that have a lot of parameters, default values, and so on.
10+
*Destructuring assignment* is a special syntax that allows us to "unpack" arrays or objects into a bunch of variables, as sometimes that's more convenient.
11+
12+
Destructuring also works great with complex functions that have a lot of parameters, default values, and so on. Soon we'll see that.
1013

1114
## Array destructuring
1215

13-
An example of how the array is destructured into variables:
16+
Here's an example of how an array is destructured into variables:
1417

1518
```js
1619
// we have an array with the name and surname
17-
let arr = ["Ilya", "Kantor"]
20+
let arr = ["John", "Smith"]
1821

1922
*!*
2023
// destructuring assignment
@@ -23,18 +26,22 @@ let arr = ["Ilya", "Kantor"]
2326
let [firstName, surname] = arr;
2427
*/!*
2528

26-
alert(firstName); // Ilya
27-
alert(surname); // Kantor
29+
alert(firstName); // John
30+
alert(surname); // Smith
2831
```
2932

3033
Now we can work with variables instead of array members.
3134

3235
It looks great when combined with `split` or other array-returning methods:
3336

34-
```js
35-
let [firstName, surname] = "Ilya Kantor".split(' ');
37+
```js run
38+
let [firstName, surname] = "John Smith".split(' ');
39+
alert(firstName); // John
40+
alert(surname); // Smith
3641
```
3742

43+
As you can see, the syntax is simple. There are several peculiar details though. Let's see more examples, to better understand it.
44+
3845
````smart header="\"Destructuring\" does not mean \"destructive\"."
3946
It's called "destructuring assignment," because it "destructurizes" by copying items into variables. But the array itself is not modified.
4047
@@ -69,26 +76,25 @@ In the code above, the second element of the array is skipped, the third one is
6976
let [a, b, c] = "abc"; // ["a", "b", "c"]
7077
let [one, two, three] = new Set([1, 2, 3]);
7178
```
72-
79+
That works, because internally a destructuring assignment works by iterating over the right value. It's kind of syntax sugar for calling `for..of` over the value to the right of `=` and assigning the values.
7380
````
7481

7582

7683
````smart header="Assign to anything at the left-side"
77-
7884
We can use any "assignables" at the left side.
7985
8086
For instance, an object property:
8187
```js run
8288
let user = {};
83-
[user.name, user.surname] = "Ilya Kantor".split(' ');
89+
[user.name, user.surname] = "John Smith".split(' ');
8490
85-
alert(user.name); // Ilya
91+
alert(user.name); // John
92+
alert(user.surname); // Smith
8693
```
8794
8895
````
8996

9097
````smart header="Looping with .entries()"
91-
9298
In the previous chapter we saw the [Object.entries(obj)](mdn:js/Object/entries) method.
9399
94100
We can use it with destructuring to loop over keys-and-values of an object:
@@ -107,62 +113,81 @@ for (let [key, value] of Object.entries(user)) {
107113
}
108114
```
109115
110-
...And the same for a map:
116+
The similar code for a `Map` is simpler, as it's iterable:
111117
112118
```js run
113119
let user = new Map();
114120
user.set("name", "John");
115121
user.set("age", "30");
116122
117123
*!*
124+
// Map iterates as [key, value] pairs, very convenient for destructuring
118125
for (let [key, value] of user) {
119126
*/!*
120127
alert(`${key}:${value}`); // name:John, then age:30
121128
}
122129
```
123130
````
124131

125-
```smart header="Swap variables trick"
126-
A well-known trick for swapping values of two variables:
132+
````smart header="Swap variables trick"
133+
There's a well-known trick for swapping values of two variables using a destructuring assignment:
127134
128135
```js run
129136
let guest = "Jane";
130137
let admin = "Pete";
131138
132-
// Swap values: make guest=Pete, admin=Jane
139+
// Let's swap the values: make guest=Pete, admin=Jane
140+
*!*
133141
[guest, admin] = [admin, guest];
142+
*/!*
134143
135144
alert(`${guest} ${admin}`); // Pete Jane (successfully swapped!)
136145
```
137146
138147
Here we create a temporary array of two variables and immediately destructure it in swapped order.
139148
140149
We can swap more than two variables this way.
141-
150+
````
142151

143152
### The rest '...'
144153

145-
If we want not just to get first values, but also to gather all that follows -- we can add one more parameter that gets "the rest" using three dots `"..."`:
154+
Usually, if the array is longer when the list at the left, the "extra" items are omitted.
155+
156+
For example, here only two items are taken, and the rest is just ignored:
146157

147158
```js run
148-
let [name1, name2, *!*...rest*/!*] = ["Julius", "Caesar", *!*"Consul", "of the Roman Republic"*/!*];
159+
let [name1, name2] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];
149160

150161
alert(name1); // Julius
151162
alert(name2); // Caesar
163+
// Furher items aren't assigned anywhere
164+
```
165+
166+
If we'd like also to gather all that follows -- we can add one more parameter that gets "the rest" using three dots `"..."`:
167+
168+
```js run
169+
let [name1, name2, *!*...rest*/!*] = ["Julius", "Caesar", *!*"Consul", "of the Roman Republic"*/!*];
152170
153171
*!*
154-
// Note that type of `rest` is Array.
172+
// rest is array of items, starting from the 3rd one
155173
alert(rest[0]); // Consul
156174
alert(rest[1]); // of the Roman Republic
157175
alert(rest.length); // 2
158176
*/!*
159177
```
160178

161-
The value of `rest` is the array of the remaining array elements. We can use any other variable name in place of `rest`, just make sure it has three dots before it and goes last in the destructuring assignment.
179+
The value of `rest` is the array of the remaining array elements.
180+
181+
We can use any other variable name in place of `rest`, just make sure it has three dots before it and goes last in the destructuring assignment.
182+
183+
```js run
184+
let [name1, name2, *!*...titles*/!*] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];
185+
// now titles = ["Consul", "of the Roman Republic"]
186+
```
162187

163188
### Default values
164189

165-
If there are fewer values in the array than variables in the assignment, there will be no error. Absent values are considered undefined:
190+
If the array is shorter than the list of variables at the left, there'll be no errors. Absent values are considered undefined:
166191

167192
```js run
168193
*!*
@@ -187,7 +212,7 @@ alert(surname); // Anonymous (default used)
187212

188213
Default values can be more complex expressions or even function calls. They are evaluated only if the value is not provided.
189214

190-
For instance, here we use the `prompt` function for two defaults. But it will run only for the missing one:
215+
For instance, here we use the `prompt` function for two defaults:
191216

192217
```js run
193218
// runs only prompt for surname
@@ -197,7 +222,7 @@ alert(name); // Julius (from array)
197222
alert(surname); // whatever prompt gets
198223
```
199224

200-
225+
Please note: the `prompt` will run only for the missing value (`surname`).
201226

202227
## Object destructuring
203228

@@ -209,7 +234,7 @@ The basic syntax is:
209234
let {var1, var2} = {var1:…, var2:…}
210235
```
211236

212-
We have an existing object at the right side, that we want to split into variables. The left side contains a "pattern" for corresponding properties. In the simple case, that's a list of variable names in `{...}`.
237+
We should have an existing object at the right side, that we want to split into variables. The left side contains an object-like "pattern" for corresponding properties. In the simplest case, that's a list of variable names in `{...}`.
213238

214239
For instance:
215240

@@ -229,7 +254,9 @@ alert(width); // 100
229254
alert(height); // 200
230255
```
231256

232-
Properties `options.title`, `options.width` and `options.height` are assigned to the corresponding variables. The order does not matter. This works too:
257+
Properties `options.title`, `options.width` and `options.height` are assigned to the corresponding variables.
258+
259+
The order does not matter. This works too:
233260

234261
```js
235262
// changed the order in let {...}
@@ -238,7 +265,7 @@ let {height, width, title} = { title: "Menu", height: 200, width: 100 }
238265

239266
The pattern on the left side may be more complex and specify the mapping between properties and variables.
240267

241-
If we want to assign a property to a variable with another name, for instance, `options.width` to go into the variable named `w`, then we can set it using a colon:
268+
If we want to assign a property to a variable with another name, for instance, make `options.width` go into the variable named `w`, then we can set the variable name using a colon:
242269

243270
```js run
244271
let options = {

1-js/06-advanced-functions/10-bind/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ The call to `mul.bind(null, 2)` creates a new function `double` that passes call
247247
248248
That's called [partial function application](https://en.wikipedia.org/wiki/Partial_application) -- we create a new function by fixing some parameters of the existing one.
249249
250-
Please note that here we actually don't use `this` here. But `bind` requires it, so we must put in something like `null`.
250+
Please note that we actually don't use `this` here. But `bind` requires it, so we must put in something like `null`.
251251
252252
The function `triple` in the code below triples the value:
253253

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class Rabbit extends Animal {
151151
let rabbit = new Rabbit("White Rabbit");
152152
153153
rabbit.run(5); // White Rabbit runs with speed 5.
154-
rabbit.stop(); // White Rabbit stands still. White rabbit hides!
154+
rabbit.stop(); // White Rabbit stands still. White Rabbit hides!
155155
```
156156
157157
Now `Rabbit` has the `stop` method that calls the parent `super.stop()` in the process.

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function curried(...args) {
155155
if (args.length >= func.length) { // (1)
156156
return func.apply(this, args);
157157
} else {
158-
return function pass(...args2) { // (2)
158+
return function(...args2) { // (2)
159159
return curried.apply(this, args.concat(args2));
160160
}
161161
}
@@ -164,18 +164,10 @@ function curried(...args) {
164164

165165
When we run it, there are two `if` execution branches:
166166

167-
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.
168-
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.
167+
1. If passed `args` count is the same or more than the original function has in its definition (`func.length`) , then just pass the call to it using `func.apply`.
168+
2. Otherwise, get a partial: we don't call `func` just yet. Instead, another wrapper is returned, that will re-apply `curried` providing previous arguments together with the new ones.
169169

170-
For instance, let's see what happens in the case of `sum(a, b, c)`. Three arguments, so `sum.length = 3`.
171-
172-
For the call `curried(1)(2)(3)`:
173-
174-
1. The first call `curried(1)` remembers `1` in its Lexical Environment, and returns a wrapper `pass`.
175-
2. The wrapper `pass` is called with `(2)`: it takes previous args (`1`), concatenates them with what it got `(2)` and calls `curried(1, 2)` with them together. As the argument count is still less than 3, `curry` returns `pass`.
176-
3. The wrapper `pass` is called again with `(3)`, for the next call `pass(3)` takes previous args (`1`, `2`) and adds `3` to them, making the call `curried(1, 2, 3)` -- there are `3` arguments at last, they are given to the original function.
177-
178-
If that's still not obvious, just trace the calls sequence in your mind or on paper.
170+
Then, if we call it, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
179171

180172
```smart header="Fixed-length functions only"
181173
The currying requires the function to have a fixed number of arguments.

0 commit comments

Comments
 (0)