Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Добавляет ответ на вопрос об отличиях rest и spread #5180

Merged
merged 5 commits into from Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.