You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/02-rest-parameters-spread-operator/article.md
+19-21Lines changed: 19 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ For instance:
8
8
-`Object.assign(dest, src1, ..., srcN)` -- copies properties from `src1..N` into `dest`.
9
9
- ...and so on.
10
10
11
-
In this chapter we'll see how to do the same. And, more important, how to feel comfortable working with such functions and arrays.
11
+
In this chapter we'll learn how to do the same. And, more importantly, how to feel comfortable working with such functions and arrays.
12
12
13
13
[cut]
14
14
@@ -27,7 +27,7 @@ alert( sum(1, 2, 3, 4, 5) );
27
27
28
28
There will be no error because of "excessive" arguments. But of course in the result only the first two will be counted.
29
29
30
-
The rest parameters can be mentioned in a function definition with three dots `...`. They literally mean: "gather the remaining parameters into an array".
30
+
The rest parameters can be mentioned in a function definition with three dots `...`. They literally mean "gather the remaining parameters into an array".
31
31
32
32
For instance, to gather all arguments into array `args`:
33
33
@@ -45,9 +45,9 @@ alert( sumAll(1, 2) ); // 3
45
45
alert( sumAll(1, 2, 3) ); // 6
46
46
```
47
47
48
-
We can choose to get first parameters as variables, and gather only the rest.
48
+
We can choose to get the first parameters as variables, and gather only the rest.
49
49
50
-
Here the first two arguments go into variables and the rest goes to`titles` array:
50
+
Here the first two arguments go into variables and the rest go into`titles` array:
@@ -72,7 +72,7 @@ function f(arg1, ...rest, arg2) { // arg2 after ...rest ?!
72
72
}
73
73
```
74
74
75
-
The `...rest` must always be the last.
75
+
The `...rest` must always be last.
76
76
````
77
77
78
78
## The "arguments" variable
@@ -98,13 +98,13 @@ showName("Julius", "Caesar");
98
98
showName("Ilya");
99
99
```
100
100
101
-
In old times, rest parameters did not exist in the language, and `arguments` was the only way to get all arguments of the function no matter of their total number.
101
+
In old times, rest parameters did not exist in the language, and using `arguments` was the only way to get all arguments of the function, no matter their total number.
102
102
103
-
And it still works, we can use it.
103
+
And it still works, we can use it today.
104
104
105
-
But the downside is that although `arguments` is both array-like and iterable, it's not an array. It does not support array methods, so we can't say call `arguments.map(...)`.
105
+
But the downside is that although `arguments` is both array-like and iterable, it's not an array. It does not support array methods, so we can't call `arguments.map(...)` for example.
106
106
107
-
Also, it always has all arguments in it, we can't capture them partially, like we did with rest parameters.
107
+
Also, it always contains all arguments. We can't capture them partially, like we did with rest parameters.
108
108
109
109
So when we need these features, then rest parameters are preferred.
110
110
@@ -123,21 +123,19 @@ f(1); // 1
123
123
```
124
124
As we remember, arrow functions don't have their own `this`. Now we know they don't have the special `arguments` object either.
125
125
126
-
````
127
-
128
126
## Spread operator [#spread-operator]
129
127
130
128
We've just seen how to get an array from the list of parameters.
131
129
132
130
But sometimes we need to do exactly the reverse.
133
131
134
-
For instance, there's a built-in function [Math.max](mdn:js/Math/max) that returns the greatest number from the list:
132
+
For instance, there's a built-in function [Math.max](mdn:js/Math/max) that returns the greatest number from a list:
135
133
136
134
```js run
137
135
alert( Math.max(3, 5, 1) ); // 5
138
136
```
139
137
140
-
Now let's say we have an array `[3, 5, 1]`. How to call `Math.max` with it?
138
+
Now let's say we have an array `[3, 5, 1]`. How do we call `Math.max` with it?
141
139
142
140
Passing it "as it" won't work, because `Math.max` expects a list of numeric arguments, not a single array:
143
141
@@ -149,9 +147,9 @@ alert( Math.max(arr) ); // NaN
149
147
*/!*
150
148
```
151
149
152
-
...And surely we can't manually list items in the code `Math.max(arg[0], arg[1], arg[2])`, because we may be unsure how much are there. As our script executes, there might be many, or there might be none. Also that would be ugly.
150
+
And surely we can't manually list items in the code `Math.max(arg[0], arg[1], arg[2])`, because we may be unsure how many there are. As our script executes, there could be a lot, or there could be none. And that would get ugly.
153
151
154
-
*Spread operator* to the rescue. It looks similar to rest parameters, also using `...`, but does quite the opposite.
152
+
*Spread operator* to the rescue! It looks similar to rest parameters, also using `...`, but does quite the opposite.
155
153
156
154
When `...arr` is used in the function call, it "expands" an iterable object `arr` into the list of arguments.
157
155
@@ -172,7 +170,7 @@ let arr2 = [8, 3, -8, 1];
172
170
alert( Math.max(...arr1, ...arr2) ); // 8
173
171
```
174
172
175
-
...And even combine the spread operator with normal values:
173
+
We can even combine the spread operator with normal values:
0 commit comments