实现数据劫持,不实现页面更新部分
const reactiveHandler={
get(target,prop){
const result=Reflect.get(target,prop);
return result;
},
set(target,prop,value){
const result=Reflect.set(target,prop,value);
console.log('===')
return result;
},
deleteProperty(target,prop){
const result=Reflect.deleteProperty(target,prop);
return result;
}
}
1、shallowReactive
function shallowReactive_(target){
if(target&&typeof target ==='object'){
return new Proxy(target,reactiveHandler)
}
return target
}
2、reactive
function reactive_(target){
if(target&&typeof target ==='object'){
if(Array.isArray(target)){
target.forEach((item,index)=>{
target[index]=reactive_(item);
})
}else{
Object.keys(target).forEach(key=>{
target[key]=reactive_(target[key])
})
}
return new Proxy(target,reactiveHandler)
}
return target
}