对象深拷贝
var obj = {
name:'zhangsan',
age:14,
};
var temp = JSON.parse(JSON.tostringify(obj));
// 代理法
function deepClone(obj) {
if (!isObject(obj)) {
throw new Error('obj 不是一个对象!')
}
let isArray = Array.isArray(obj)
let cloneObj = isArray ? [...obj] : { ...obj }
Reflect.ownKeys(cloneObj).forEach(key => {
cloneObj[key] = isObject(obj[key]) ? deepClone(obj[key]) : obj[key]
})
return cloneObj
}
对象是否相等
var obj1 = {name="zhangsan"}
var obj2 = {name="zhangsan"}
JSON.stringify(obj1) == JSON.stringify(obj2);
将一个对象的数据加入到另一个对象中
var obj1 = {name:"zhangsan"}
var obj2 = {age:18}
var obj3 = {
...obj1,
...obj2
}