原生深拷贝

// 封装一个函数,拷贝传入的数据

    function deepClone(data) {
        if ((typeof data != "object" && typeof data != "function") || data === null) { // 把基本数据类型过滤出来
            return data;
        }
        if (typeof data === "function") { // 处理函数
            return data.bind();
        }
        // 处理对象和数组
        var res = data instanceof Array ? [] : {};
        Object.entires(data).forEach((key,value)){
			//重复执行以上过程,让对象或数组中的对象和数组也参与深拷贝
            res[key] = deepClone(value);
		}
        }
        return res;
    }
posted @ 2022-03-25 10:01  听风小弟  阅读(30)  评论(0)    收藏  举报