UNPKG

lbr-tool

Version:

## js工具库

29 lines (25 loc) 651 B
type IDeepClone = (obj:object) => object // 深拷贝 const deepClone = (obj):IDeepClone => { if (typeof obj !== 'object' || obj == null) { // obj 是 null ,或者不是对象和数组,直接返回 return obj } // 初始化返回结果 let result if (obj instanceof Array) { result = [] } else { result = {} } for (let key in obj) { // 保证 key 不是原型的属性 if (obj.hasOwnProperty(key)) { // 递归调用!!! result[key] = deepClone(obj[key]) } } // 返回结果 return result } export default deepClone