深复制

19 阅读1分钟
深复制就是将对象中的每层的引用关系都不同,每个属性改变都不会影响复制好的对象
JSON无法转换Symbol属性,无法转换方法的,无法转换不可枚举属性


要复制的内容

    var d = Symbol();

        var e = Symbol();

        var ss = {

            a: 1

        };

        var date = new Date();

        var divs = document.createElement("div");

        divs.setAttribute("a", "3");

        date.setFullYear(2024);




        function Box() {




        }

        Box.prototype.a = 10;

        Box.prototype.play = function () {

            console.log("aaa");

        }





        var obj = {

            a: 1,

            b: 2,

            c: [1, 2, 3],

            zz: new Set([1, 2, ss]),

            yy: new Map(),

            [d]: "aaa",

            z: divs,

            d: {

                e: date,

                f: /a/g,

                g: function (s) {

                    console.log(s);

                },

                h: {},

            },

            ttt: Box

        };

        obj.d.g.ssss = 10;

        obj.eee = new Uint8Array([97, 98, 99]);

        Object.defineProperties(obj.d.h, {

            i: {

                value: 10,

            },

            j: {

                configurable: true,

                writable: true,

                value: [1, 2, 3, 4],

            },

            k: {

                writable: true,

                value: {

                    l: {},

                    m: "abcde",

                    n: true,

                    o: [1, 2, 3],

                },

            },

            [e]: {

                value: ["a", "b", "c", "e"],

            },

        });

        obj.z.style.width = "50px";

        obj.z.style.height = "50px";

        obj.z.style.backgroundColor = "red";




        Object.defineProperties(obj.d.h.k.l, {

            p: {

                value: function (a,b) {

                    console.log("p");

                },

            },

            q: {

                value: {

                    r: {

                        a: 1

                    },

                    j: {

                        b: 2

                    },

                },

            },

        });

        var a_1 = {

            a: 1

        };

        var a_2 = {

            b: 2

        };

        obj.yy.set("name", "xietian");

        obj.yy.set(a_1, a_2);




        Object.defineProperty(obj, "www", {

            set: function (_v) {

                this.a = _v;

            },

            get: function () {

                return this.a;

            },

        });
        
        
        
        

深复制

            function cloneDeep(source,target){

            switch(true){

                case source instanceof HTMLElement:

                    target=source.cloneNode(true);

                    break;

                case source.constructor.__proto__.name==="TypedArray":

                case source instanceof Set:

                case source instanceof Map:

                case source instanceof Date:

                case source instanceof RegExp:

                    target=new source.constructor(source);

                    break;

                default:

                   target=new source.constructor()

            }

            var names=Reflect.ownKeys(source);

            for(var i=0;i<names.length;i++){

                var key=names[i];

                var desc=Object.getOwnPropertyDescriptor(source,key);

                // console.log(key,desc,desc.value)

                if(typeof desc.value==="object" && desc.value!==null){

                    Object.defineProperty(target,key,{

                        configurable:desc.configurable,

                        writable:desc.writable,

                        enumerable:desc.enumerable,

                        value:cloneDeep(desc.value)

                    })

                }else{

                    Object.defineProperty(target,key,desc);

                }

            }

            return target;

        }




      var target= cloneDeep(obj)

      obj.d.h.k.l.q.j.b=1000;

        obj.ttt.prototype.a=100;

      console.log(target,obj)