• JavaScript 实现深度拷贝


    var obj = {
                name: "abc",
                age: 123,
                card: ['visa', 'master'],
                wife: {
                    name: 'bcd',
                    son: {
                        name: 'aaa'
                    }
                }
            }
            var obj1 = {
    
            }
            //深度拷贝
            function deepClone(origin, target) {
                var target = target || {},
                    toStr = Object.prototype.toString,
                    arrStr = "[object Array]";
                for (var prop in origin) {
                    if (origin.hasOwnProperty(prop)) {
                        if (origin[prop] !== "null" && typeof (origin[prop]) == 'object') {
                            // if (toStr.call(origin[prop]) == arrStr) {
                            //     target[prop] = [];
                            // } else {
                            //     target[prop] = {};
                            // }
                            target[prop] = toStr.call(origin[prop]) == arrStr ? [] : {};
                            deepClone(origin[prop], target[prop]);
                        } else {
                            target[prop] = origin[prop];
                        }
                    }
                }
                return target;
            }
            deepClone(obj, obj1);
  • 相关阅读:
    撒谎
    可怜的猪
    GIS学习笔记(五)
    国产木马冰河2.2
    矛盾
    GIS学习笔记(六)
    男人如衣服
    VS2005快捷键大全
    慧悟
    DOS命令
  • 原文地址:https://www.cnblogs.com/FashionDoo/p/10631693.html
Copyright © 2020-2023  润新知