-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
142vip.cn
committed
Nov 8, 2023
1 parent
1ea8d51
commit f11753d
Showing
32 changed files
with
716 additions
and
791 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* 深拷贝实现 | ||
*/ | ||
|
||
const obj = { | ||
name: 'xxxx', | ||
test: 'xxx', | ||
// 浅拷贝中,这一层只能拷贝引用 | ||
msg: { | ||
name: 'xxx' | ||
}, | ||
color: ['pink', 'red'] | ||
} | ||
|
||
const obj_test = { | ||
|
||
} | ||
|
||
// 利用函数递归来实现深拷贝 | ||
function deepCopy(newObj, oldObj) { | ||
for (const key in oldObj) { | ||
// 判断我们的属性值属于那种数据类型 | ||
const item = oldObj[key] | ||
// 判断item是否为数组 | ||
if (item instanceof Array) { | ||
newObj[key] = [] | ||
deepCopy(newObj[key], item) | ||
// 注意 Array instanceof Object 数组是属于对象的 先过滤 | ||
} else if (item instanceof Object) { | ||
// 判断item是否为对象 | ||
newObj[key] = {} | ||
deepCopy(newObj[key], item) | ||
} else { | ||
// 判断是否为简单数据类型 | ||
newObj[key] = item | ||
} | ||
} | ||
} | ||
|
||
deepCopy(obj_test, obj) | ||
// 输出对比 | ||
console.log(obj_test, obj) |
Oops, something went wrong.