Files
freeCodeCamp/curriculum/challenges/chinese/22-rosetta-code/rosetta-code-challenges/deepcopy.md
2024-01-24 19:52:36 +01:00

1.4 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
596a8888ab7c01048de257d5 深度拷贝 1 302247 deepcopy

--description--

Write a function that returns a deep copy of a given object. The copy must not be the same object that was given.

此任务不会测试:

  • Objects with properties that are functions
  • Date 对象或具有 Date 对象属性的对象
  • RegEx 或具有作为 RegEx 对象的属性的对象
  • 原型复制

--hints--

deepcopy 应该是一个函数。

assert(typeof deepcopy === 'function');

deepcopy({test: "test"}) 应该返回一个对象。

assert(typeof deepcopy(obj1) === 'object');

deepcopy 不应返回与提供的相同对象。

assert(deepcopy(obj2) != obj2);

当传递一个包含数组的对象时,deepcopy 应该返回该对象的深层副本。

assert.deepEqual(deepcopy(obj2), obj2);

当传递包含另一个对象的对象时,deepcopy 应返回该对象的深层副本。

assert.deepEqual(deepcopy(obj3), obj3);

--seed--

--after-user-code--

const obj1 = { test: 'test' };
const obj2 = {
  t: 'test',
  a: ['an', 'array']
};
const obj3 = {
  t: 'try',
  o: obj2
};

--seed-contents--

function deepcopy(obj) {

  return true;
}

--solutions--

function deepcopy(obj) {
  return JSON.parse(JSON.stringify(obj));
}