关于JavaScript深拷贝封装

310 阅读1分钟
        var initCopy = (function () {
            function Copy() {
                this.type = Object.prototype.toString;
                this.typeReference = 'object';
                this.typeObj = '[object Object]';
                this.typeArray = '[object Array]';
            }
            Copy.prototype.copy = function (target, current) {
                if (!target) return null;
                if (typeof target != this.typeReference) return target;
                current = current ? current : {};
                for (let key in target) {
                    current[key] = this.type.call(target[key]) == this.typeObj ? {} : [];
                    current[key] = this.copy(target[key], current[key]);
                }
                return current;
            }
            return Copy;
        }());