两个对象实现浅层比较
let obj = {
a:20
}
let obj1 = {
b:30,
c:obj,
arr:[10,20,30]
}
let obj2 = {
b:40,
c:obj,
arr:[10,20,30]
}
const isObject = (obj)=>{
return obj!==null && /^(object)|function$/.test(typeof obj);
}
const shallowEqual = function shallowEqual(obj1,obj2){
if(!isObject(obj1) || !isObject(obj2)) return false
let keys1 = Reflect.ownKeys(obj1),
keys2 = Reflect.ownKeys(obj2)
if(keys1.length !==keys2.length) return false
for(let i = 0;i<keys1.length;i++){
let key = keys1[i]
if(!obj2.hasOwnProperty(key) || !Object.is(obj1[key],obj2[key])){
return false
}
}
return true
}
shallowEqual(obj1,obj2)