[蚂蚁集团]使用 JavaScript Proxy实现简单的数据绑定
webVueBlog opened this issue · comments
I am ne zha / Jeskson commented
lyric 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);
}
})