2018-10-10 18:03:03 -04:00
---
title: Deepcopy
id: 596a8888ab7c01048de257d5
challengeType: 5
2019-08-28 16:26:13 +03:00
forumTopicId: 302247
2018-10-10 18:03:03 -04:00
localeTitle: DeepCopy
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
Задача: < p > Напишите функцию, которая возвращает глубокую копию данного объекта. < / p > < p > Копия не должна быть тем же самым объектом, который был дан. < / p > < p > Эта задача не будет проверяться: < / p > Объекты с о свойствами, которые являются функциями. Объекты Date или объект с о свойствами, которые являются объектами Date. RegEx или объект с о свойствами, которые являются объектами RegEx. Прототипирование копий
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
2018-10-10 18:03:03 -04:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > deepcopy</ code > should be a function.
testString: assert(typeof deepcopy === 'function');
- text: '< code > deepcopy({test: "test"})</ code > should return an object.'
testString: assert(typeof deepcopy(obj1) === 'object');
- text: Should not return the same object that was provided.
testString: assert(deepcopy(obj2) != obj2);
- text: When passed an object containing an array, should return a deep copy of the object.
testString: assert.deepEqual(deepcopy(obj2), obj2);
- text: When passed an object containing another object, should return a deep copy of the object.
testString: assert.deepEqual(deepcopy(obj3), obj3);
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-08-28 16:26:13 +03:00
function deepcopy(obj) {
2018-10-10 18:03:03 -04:00
// Good luck!
return true;
}
```
< / div >
2019-08-28 16:26:13 +03:00
### After Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-teardown' >
```js
2019-08-28 16:26:13 +03:00
const obj1 = { test: 'test' };
const obj2 = {
t: 'test',
a: ['an', 'array']
};
const obj3 = {
t: 'try',
o: obj2
};
2018-10-10 18:03:03 -04:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function deepcopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >