var obj ={
a:'hello',
name:{
a:'你的',
b:'我的'
},
c:'world'
}
浅拷贝:
1.
function simpleClone(objNew){
var obj = {}
for(let i in objNew){
obj[i] = objNew[i]
}
return obj
}
var objCopy = simpleClone(obj)
2.
var CObj = Object.create(obj)
深拷贝
1.
function deepCopy(newobj,endobj){
let obj = endobj || {}
for(let i in newobj){
if(typeof newobj[i] === 'object'){
console.log(11)
obj[i] = newobj[i].constructor === Array?[]:{}
deepCopy(newobj[i] ,obj[i])
}else{
obj[i] = newobj[i]
}
}
return obj
}
var deep = deepCopy(obj)
2.
var deep = JSON.parse(JSON.stringify(obj))
deep.name.a="hello1"
console.log(deep)
console.log(obj)
浅拷贝的两种方式 ,深拷贝的两种方式。