• js对象深拷贝


    /**
    *对象深拷贝2018-3-2
    *使用方法deepAssign(obj1,obj2...)
    **/        
    		
    		
    		
    		//测试
    		//var china = {
            //    nation : '中国',
            //    birthplaces:['北京','上海','广州'],
            //    skincolr :{aa:111,bb:{qq:{hahha:{kkkk:"哈哈哈"}}}},
            //    friends:['sk','ls']
           // }
           // var result = {name:'result', skincolr :{aa:111,bb:{qq:{hahha:{kkkk:"可以啦啦啦啦"}}}}}
    
            //深复制,要想达到深复制就需要用递归
            function isObj(x){
                var type = typeof x;
                return x !== null && (type === 'object' || type === 'function');
            }
    
            var hasOwnProperty = Object.prototype.hasOwnProperty;
            var propIsEnumerable = Object.prototype.propertyIsEnumerable;
    
            function toObject(val) {
                if (val === null || val === undefined) {
                    throw new TypeError('Cannot convert undefined or null to object');
                }
    
                return Object(val);
            }
    
            function assignKey(to, from, key) {
                var val = from[key];
    
                if (val === undefined || val === null) {
                    return;
                }
    
                if (hasOwnProperty.call(to, key)) {
                    if (to[key] === undefined || to[key] === null) {
                        throw new TypeError('Cannot convert undefined or null to object (' + key + ')');
                    }
                }
    
                if (!hasOwnProperty.call(to, key) || !isObj(val)) {
                    to[key] = val;
                } else {
                    to[key] = assign(Object(to[key]), from[key]);
                }
            }
    
            function assign(to, from) {
                if (to === from) {
                    return to;
                }
    
                from = Object(from);
    
                for (var key in from) {
                    if (hasOwnProperty.call(from, key)) {
                        assignKey(to, from, key);
                    }
                }
    
                if (Object.getOwnPropertySymbols) {
                    var symbols = Object.getOwnPropertySymbols(from);
    
                    for (var i = 0; i < symbols.length; i++) {
                        if (propIsEnumerable.call(from, symbols[i])) {
                            assignKey(to, from, symbols[i]);
                        }
                    }
                }
    
                return to;
            }
    
            function deepAssign(target) {
                target = toObject(target);
    
                for (var s = 1; s < arguments.length; s++) {
                    assign(target, arguments[s]);
                }
    
                return target;
            };
           // deepAssign(china,result)
    		//console.log(china)
    

      

  • 相关阅读:
    242. Valid Anagram
    [wikioi]关押罪犯
    [wikioi]数的划分
    [wikioi]能量项链
    [wikioi]线段覆盖 2
    [wikioi]乌龟棋
    POJ1011 Sticks
    *[leetcode]Word Break II
    [leetcode]Word Break
    《高性能网站建设指南》笔记
  • 原文地址:https://www.cnblogs.com/holy-amy/p/8492295.html
Copyright © 2020-2023  润新知