Skip to content

Commit

Permalink
Добавляет ответ на вопрос об отличиях rest и spread (#5180)
Browse files Browse the repository at this point in the history
Co-authored-by: Polina Gurtovaia <zloymult@gmail.com>
Co-authored-by: Tatiana Fokina <fokinatatian@gmail.com>
  • Loading branch information
3 people committed Apr 2, 2024
1 parent 831afb3 commit 06798d5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
81 changes: 81 additions & 0 deletions interviews/rest-spread/answers/iren-a/index.md
@@ -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]
```

8 changes: 8 additions & 0 deletions people/iren-a/index.md
@@ -0,0 +1,8 @@
---
name: 'Ирина Аринушкина'
url: https://github.com/iren-a
photo: photo.jpeg
badges:
- first-contribution-small
---

Binary file added people/iren-a/photo.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 06798d5

Please sign in to comment.