Skip to content

Commit be16ed9

Browse files
committed
up
1 parent b2ec76d commit be16ed9

File tree

312 files changed

+2735
-774
lines changed

Some content is hidden

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

312 files changed

+2735
-774
lines changed

1-js/2-first-steps/15-while-for/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ label: for(...)
368368
The call to a `break/continue` is only possible from inside the loop, and the label must be somewhere upwards from the directive.
369369
````
370370

371-
## The "for..in" over objects
371+
## The "for..in" over objects [#for..in]
372372

373373
To walk over all keys of an object, there exists a special form of the loop: `for..in`. This is a completely different thing from the `for(;;)` construct that we've studied before.
374374

1-js/2-first-steps/17-function-basics/article.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,12 @@ sumAll(1, 2); // 3
261261
sumAll(1, 2, 3); // 6
262262
```
263263
264-
Here `...args` means "gather all parameters" into an array `args`.
264+
Here `...args` means "gather all parameters into an array `args`".
265265
266266
We also can put few first arguments into variables and gather only the rest:
267267
268268
```js run
269+
// titles will be an array of arguments from the 3rd one
269270
function showName(firstName, lastName, ...titles) {
270271
alert( firstName + ' ' + lastName ); // Julius Caesar
271272

@@ -277,7 +278,7 @@ showName("Julius", "Caesar", "Consul", "Praetor", "Imperator");
277278
```
278279
279280
````warn header="The rest operator `...` must be the last"
280-
The rest operator `` gathers all remaining arguments, so the following has no sense:
281+
The rest operator `...` gathers all remaining arguments, so the following has no sense:
281282
282283
```js
283284
function f(arg1, ...rest, arg2) { // arg2 after ...rest ?!
@@ -305,18 +306,22 @@ showName("Julius", "Caesar");
305306
306307
We can also use `for..of` to iterate over it.
307308
308-
The downside is that `arguments` looks like an array, but it's not so. It does not support many useful array methods that we'll study later. Also we can't use it to capture "the rest" of arguments, it always has all of them.
309+
The downside is that `arguments` looks like an array, but it's not a real array. It does not support many useful array methods that we'll study later. Also we can't use it to capture "the rest" of arguments starting from the given one.
309310
````
310311
311312
### The spread operator [#spread-operator]
312313
313314
The rest operator `...` allows to gather parameters in the array.
314315
315-
But there's a reverse operator named "the spread". It also looks like `...` and allows to convert an array into a list of parameters, like this:
316+
But there's a reverse operator named "the spread". It has the same form three dots `...` and allows to pass an array as a list of parameters in a function call.
317+
318+
Like this:
316319
317320
```js run
321+
// we have array
318322
let fullName = ["Gaius", "Julius", "Caesar"];
319323

324+
// the function has a list of arguments
320325
function showName(firstName, secondName, lastName) {
321326
alert(firstName);
322327
alert(secondName);
@@ -327,9 +332,11 @@ function showName(firstName, secondName, lastName) {
327332
showName(...fullName);
328333
```
329334
330-
As you can see, there are same three dots as the rest operator. But the meaning is different.
335+
To put it clear, if we used `showName(fullName)`, the function would receive a single argument -- the array `fullName`. The spread operator `showName(...fullName)` "unwraps" the array into multiple arguments.
336+
337+
As we can see, the rest and spread operators look the same. But the meaning is different.
331338
332-
To put it simple:
339+
There's a simple rule to distinguish them:
333340
- When `...` occurs in function parameters, it's called a "rest operator" and gathers parameters into the array.
334341
- When `...` occurs in a function call, it's called a "spread operator" and passes an array as the list of parameters.
335342
@@ -339,7 +346,7 @@ When that may be useful?
339346
340347
Let's see a more real-life example.
341348
342-
There exist a built-in function [Math.max](mdn:js/Math/max) that takes a list of values and returns the greatest one:
349+
There exists a built-in function [Math.max](mdn:js/Math/max) that takes a list of values and returns the greatest one:
343350
344351
```js run
345352
alert( Math.max(5, 7, -8, 1) ); // 7
@@ -354,6 +361,27 @@ let arr = [5, 7, -8, 1];
354361
alert( Math.max(...arr) ); // 7
355362
```
356363
364+
We also can pass multiple arrays this way:
365+
366+
```js run
367+
let arr1 = [1, -2, 3, 4];
368+
let arr2 = [8, 3, -8, 1];
369+
370+
alert( Math.max(...arr1, ...arr2) ); // 8
371+
```
372+
373+
...And even combine the spread operator with normal values:
374+
375+
376+
```js run
377+
let arr1 = [1, -2, 3, 4];
378+
let arr2 = [8, 3, -8, 1];
379+
380+
alert( Math.max(1, ...arr1, 2, ...arr2, 25) ); // 25
381+
```
382+
383+
384+
357385
## Returning a value
358386
359387
A function can return a value back into the calling code as the result.

1-js/2-first-steps/21-object-tostring-valueof/article.md

Lines changed: 0 additions & 194 deletions
This file was deleted.

0 commit comments

Comments
 (0)