|
@@ -79,7 +79,7 @@ export function isValidPhoneNumber(value) {
|
|
|
return reg.test(value)
|
|
|
}
|
|
|
|
|
|
-// 深拷贝
|
|
|
+// 深拷贝,为了解决循环引用和共同引用的问题,引入了WeakMap,又因为引入WeakMap可能会导致被拷贝对象被挂上【作为WeakMap的探针的】匿名函数(是pollyfill的行为吧?),所以不会拷贝非根元素的匿名函数。
|
|
|
export function deepClone(target, hash = new WeakMap()) {
|
|
|
// 定义一个变量
|
|
|
let result = null
|
|
@@ -91,8 +91,10 @@ export function deepClone(target, hash = new WeakMap()) {
|
|
|
result = [] // 将result赋值为一个数组,并且执行遍历
|
|
|
hash.set(target, result)
|
|
|
for (let i in target) {
|
|
|
- // 递归克隆数组中的每一项
|
|
|
- result.push(deepClone(target[i], hash))
|
|
|
+ if (!(typeof(target[i]) === 'function' && !target.name)) {
|
|
|
+ // 递归克隆数组中的每一项
|
|
|
+ result.push(deepClone(target[i], hash))
|
|
|
+ }
|
|
|
}
|
|
|
// 判断如果当前的值是null的话;直接赋值为null
|
|
|
} else if (target === null) {
|
|
@@ -105,11 +107,14 @@ export function deepClone(target, hash = new WeakMap()) {
|
|
|
result = {}
|
|
|
hash.set(target, result)
|
|
|
for (let i in target) {
|
|
|
- result[i] = deepClone(target[i], hash)
|
|
|
+ if (!(typeof(target[i]) === 'function' && !target.name)) {
|
|
|
+ result[i] = deepClone(target[i], hash)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- // 如果不是对象的话,就是基本数据类型,那么直接赋值
|
|
|
- } else {
|
|
|
+ } else if (typeof target === 'function') {
|
|
|
+ result = target
|
|
|
+ } else { // 如果不是对象也不是函数,直接赋值
|
|
|
result = target
|
|
|
}
|
|
|
// 返回最终结果
|