let obj = {
a: 111
}
// 递归给对象的属性劫持set方法
function watch(obj, fn) {
Object.keys(obj).forEach(key => {
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
set(newVal) {
fn(key, newVal)
}
})
})
}
// 使用
watch(obj, (prop, newVal) => console.log(prop, newVal))
obj.a = 777
console.log(obj)
你没有设置get