【发布时间】:2017-12-16 00:15:20
【问题描述】:
我正在用 ES6 中的 Jest 为 React Components 编写一些测试。在一项测试中,我需要克隆一个导入的 json 并对克隆的对象进行变异,但是当我对克隆的对象进行变异时,原始对象也会发生变异!
import obj from './object.json'; // obj = { name: 'someName' }
describe('Testing a component', () => {
it('Some testing', () => {
const obj2 = Object.assign({}, obj); //Clone the object
obj2.name = 'otherName'; // Muatate the object
console.log(obj); // { name: 'otherName' }
});
})
为什么会这样?为什么当我对克隆的对象进行变异时,原始导入的对象也会发生变异?
【问题讨论】:
-
做一个深度克隆
标签: javascript object testing jestjs mutation