js 深度拷贝

            function cloneObj(source, target) {
                var target = target || {}
                for (let key in source) {
                    if (source.hasOwnProperty(key)) {
                        if (typeof(source[key])== 'object') {
                            target[key]=source[key] instanceof Array?[]:{}
                            cloneObj(source[key],target[key])
                        } else {
                            target[key] = source[key]
                        }
                    }
                }
                return target
            }

 

posted @ 2022-01-10 17:06  howhy  阅读(24)  评论(0编辑  收藏  举报