nicenote/docs/fea/js/index.md
2021-11-22 13:24:19 +08:00

70 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
nav:
title: 前端
path: /fea
group:
title: 💊 JavaScript
order: 2
path: /js
---
# 💊 JavaScript
## 深拷贝 VS 浅拷贝
> 区别:浅拷贝只拷贝一层,深拷贝无限层级拷贝
### 浅拷贝(方案 1
遍历
```js
function shallowClone(source) {
var target = {};
for (var i in source) {
if (source.hasOwnProperty(i)) {
target[i] = source[i];
}
}
return target;
}
```
### 深拷贝(方案 1
遍历 + 递归
**该方法出现的问题是:爆栈。当层级太深的时候会发生**
```js
function isObject(x) {
return Object.prototype.toString.call(x) === '[object Object]';
}
function deepClone(source) {
if (!isObject(source)) return source;
var target = {};
for (var i in source) {
if (source.hasOwnProperty(i)) {
if (typeof source[i] === 'object') {
target[i] = deepClone(source[i]); // 注意这里
} else {
target[i] = source[i];
}
}
}
return target;
}
```
### 深拷贝(方案 2
```js
function cloneJSON(source) {
return JSON.parse(JSON.stringify(source));
}
```