/**
* 深拷贝
* @param {Object} obj 要拷贝的对象
* obj.hasOwnProperty(key)报错,更改为Object.prototype.hasOwnProperty(obj,key)
*/
function deepClone(obj = {}) {
if (typeof obj !== 'object' || obj == null) {
// obj 是 null ,或者不是对象和数组,直接返回
return obj
}
// 初始化返回结果
let result
if (obj instanceof Array) {
result = []
} else {
result = {}
}
for (let key in obj) {
// 保证 key 不是原型的属性
if (Object.prototype.hasOwnProperty(obj,key)) {
// 递归调用!!!
result[key] = deepClone(obj[key])
}
}
// 返回结果
return result
}
/**
* 浅拷贝
*/
var peopleObj={
name:'ming',
age:12
}
var copyPeople = {}
Object.assign(copyPeople,peopleObj)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。