Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Добавляет ответ на вопрос об отличиях rest и spread (#5180)
Co-authored-by: Polina Gurtovaia <zloymult@gmail.com> Co-authored-by: Tatiana Fokina <fokinatatian@gmail.com>
- Loading branch information
1 parent
831afb3
commit 06798d5
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
Операторы `spread` и `rest` выглядят одинаково, как три точки — `...`, но выполняют противоположные действия. | ||
|
||
#### Что делает оператор `rest`? | ||
|
||
Собирает оставшиеся аргументы функции в массив. Этот синтаксис используется при объявлении функции. | ||
|
||
```js | ||
function doSomething(first, second, ...array) { | ||
console.log(first, second, array) | ||
} | ||
doSomething(1, 2, 3, 4, 5) | ||
// 1 2 [3, 4, 5] | ||
``` | ||
|
||
Собирает оставшиеся элементы массива в новый массив. Используется при деструктуризации массива. | ||
|
||
```js | ||
const [first, second, ...array] = [1, 2, 3, 4, 5] | ||
console.log(first, second, array) | ||
// 1 2 [3, 4, 5] | ||
``` | ||
|
||
Собирает оставшиеся свойства объекта в новый объект. Используется при деструктуризации объекта. | ||
|
||
```js | ||
const { c, ...object } = { a: 1, b: 2, c: 3 } | ||
console.log(c, object) | ||
// 3 {a: 1, b: 2} | ||
``` | ||
|
||
⛔ Оператор `rest` собирает все оставшиеся элементы, свойства или аргументы функции, если написать что-то после них, это вызовет ошибку: | ||
|
||
```js | ||
const [first, second, ...array, five] = [1, 2, 3, 4, 5] | ||
// five после ...array | ||
// Ошибка! | ||
``` | ||
|
||
#### Что делает оператор `spread`? | ||
|
||
Раскладывает массив на отдельные аргументы при вызове функции. | ||
|
||
```js | ||
function doSomething(first, second, third) { | ||
console.log(first, second, third) | ||
} | ||
const array = [1, 2, 3] | ||
doSomething(...array) | ||
// 1 2 3 | ||
``` | ||
|
||
Раскладывает массив на отдельные элементы. Этот синтаксис используется при создании массива с помощью литерала `[]`. | ||
|
||
```js | ||
const array = [3, 4] | ||
const newArray = [1, 2, ...array, 5] | ||
console.log(newArray) | ||
// [1, 2, 3, 4, 5] | ||
``` | ||
|
||
Раскладывает объект на отдельные свойства. Этот синтаксис используется при создании объекта с помощью литерала `{}`. | ||
|
||
```js | ||
const object = { b: 2, c: 3 } | ||
const newObject = { a: 1, ...object } | ||
console.log(newObject) | ||
// { a: 1, b: 2, c: 3} | ||
``` | ||
|
||
Оператор `spread` может стоять где угодно и использоваться любое количество раз в рамках одного вызова функции или создания массива, объекта. | ||
|
||
Так тоже можно: | ||
|
||
```js | ||
const firstArray = [3, 4] | ||
const secondArray = [15, 16] | ||
const newArray = [1, 2, ...firstArray, 5, ...secondArray] | ||
console.log(newArray) | ||
// [1, 2, 3, 4, 5, 15, 16] | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
name: 'Ирина Аринушкина' | ||
url: https://github.com/iren-a | ||
photo: photo.jpeg | ||
badges: | ||
- first-contribution-small | ||
--- | ||
|
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.