# deepClone
深拷贝对象
import { deepClone } from '@hui-pro/utils';
/* 深拷贝 */
const obj = {
test: { a: 1 }
};
const obj2 = deepClone(obj);
obj2.test.a = 2;
// obj => {
// test: { a: 1 }
// }
// obj2 => {
// test: { a: 2 }
// }
/* 浅拷贝 */
const obj = {
test: { a: 1 }
};
const obj2 = Object.assign(obj);
obj2.test.a = 2;
obj3.test2 = 'test2';
// obj => {
// test: { a: 2 }
// }
// obj2 => {
// test: { a: 2 },
// test2: 'test2'
// }
TIP
浅拷贝可使用 Object.assign({}, obj)
# API
参数 | 说明 | 类型 | 默认值 | 可选值 |
---|---|---|---|---|
obj | 要拷贝的对象 | Object | - | - |