阿孜去面试-代理

53 阅读1分钟

Skaarf_Sparkler_Gold_SE.jpg

>> Vue3 中的响应式数据是基于 Proxy 实现的,简单说说吧

<< 代理就是在操作之前对操作加以控制

  • 跟踪数据访问
  • 隐藏属性
  • 验证属性
  • 函数和构造函数参数验证
  • 数据绑定与可观察对象
// 可观察对象
const userList = []

const emit = (newValue) => {
  console.log(newValue);
}

const proxy = new Proxy(userList,{
  set(target,property,value,receiver){
    const result = Reflect.set(...arguments)
    if(result){
      emit(Reflect.get(target,property,receiver))
      return result
    }
  }
})

proxy.push('john') //john
proxy.push('jacob') // jacob