mqyqingfeng / frontend-interview-question-and-answer

前端校招面试题和答案

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[蚂蚁集团]使用 JavaScript Proxy实现简单的数据绑定

webVueBlog opened this issue · comments

commented
const scope = {} // 副作用对象
let current = null // 记录当前的副作用
const proxy = new Proxy({
  get(target, propKey, receiver){
    (scope[propKey] || scope[propKey] = []).push(current)
    return Reflect.get(target, propKey, receiver);
  },
  set(target, propKey, value, receiver) {
    (scope[propKey] || []).forEach(fn => fn(value))
    return Reflect.set(target, propKey, value, receiver);
  }
})