JS对象深拷贝

时间:2022-12-05 19:53:21
        // 对象深拷贝(数组和对象)
        function deepClone(resource) {
            if (Array.isArray(resource)) {
                let dest = []
                let length = resource.length
                while (length--) {
                    console.log(length)
                    dest[length] = deepClone(resource[length])
                }
                return dest
            } else if (typeof resource === 'object') {
                let dest = {}
                for (let key in resource) {
                    if (resource.hasOwnProperty(key)) {
                        dest[key] = deepClone(resource[key])
                    }
                }
                return dest
            }
            return resource
        }

https://www.cnblogs.com/dabingqi/p/8502932.html