什么是深拷贝,下面这个图上,两个对象没有任何一处引用相同的地址就是深拷贝,除了这种情况都是浅拷贝。
下面的红线连接的两处如果是同一个引用,所以不是深拷贝
方法一:JSON
const b = JSON.parse(JSON.stringify(a))
缺点:
- 不支持Date、正则、undefined、函数等数据
-
方法二:递归
要点:
递归
- 判断类型
- 检查环
- 不拷贝原型上的属性
```javascript
const deepClone = (a, cache) => {
if(!cache){
cache = new Map() // 缓存不能全局,最好临时创建并递归传递
}
if(a instanceof Object) { // 不考虑跨 iframe
if(cache.get(a)) { return cache.get(a) }
let result
if(a instanceof Function) {
if(a.prototype) { // 有 prototype 就是普通函数
} else {result = function(){ return a.apply(this, arguments) }
} } else if(a instanceof Array) { result = [] } else if(a instanceof Date) { result = new Date(a - 0) } else if(a instanceof RegExp) { result = new RegExp(a.source, a.flags) } else { result = {} } cache.set(a, result) for(let key in a) { if(a.hasOwnProperty(key)){result = (...args) => { return a.call(undefined, ...args) }
} } return result } else { return a } }result[key] = deepClone(a[key], cache)
const a = { number:1, bool:false, str: ‘hi’, empty1: undefined, empty2: null, array: [ {name: ‘frank’, age: 18}, {name: ‘jacky’, age: 19} ], date: new Date(2000,0,1,20,30,0), regex: /.(j|t)sx/i, obj: { name:’frank’, age: 18}, f1: (a, b) => a + b, f2: function(a, b) { return a + b } } a.self = a
const b = deepClone(a)
b.self === b // true b.self = ‘hi’ a.self !== ‘hi’ //true ```
