美文网首页
自己封装一个深拷贝,创建一个新对象,不会有传地址,共用地址空间问

自己封装一个深拷贝,创建一个新对象,不会有传地址,共用地址空间问

作者: jesse28 | 来源:发表于2021-10-07 17:39 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
// 简单数据类型 是传值Number、String、Boolean、Null、Undefined ;  复杂是传址Array、Date、Math、RegExp、Object、Function等;

let myproto = {
    name:"张三",
    age:20,
    test:undefined,
    arr:[1,3],
    fn:function(){
        console.log("fn...")
    },
    obj:{
       height:"178cm" 
    },
    test2:null
}
// 深拷贝
// let sonProto = JSON.parse(JSON.stringify(myproto));
let sonProto = deepCopy(myproto);
sonProto.name = "李四";
console.log(myproto,sonProto);

// 深拷贝函数;
function deepCopy(obj){
    let newObj = Array.isArray(obj)?[]:{};
    //循环原型上的东西;
    for(let i in obj){
        if(obj.hasOwnProperty(i)){
            if(typeof obj[i] == "object"){
                newObj[i] =  deepCopy(obj[i]);
            }else{
                newObj[i] = obj[i];
            }
        }
    }
    return newObj;
}

// let obj = {
//     name:"张三"
// }
// obj.__proto__.age = 20;
// for(let i in obj){
//     console.log(obj[i]);
// }


</script>
</html>

相关文章

网友评论

      本文标题:自己封装一个深拷贝,创建一个新对象,不会有传地址,共用地址空间问

      本文链接:https://www.haomeiwen.com/subject/hosznltx.html