//es6实现 借助set实现
function obfunction(oldval,newval){
console.log(oldval+'变成了'+newval);
}
class targetObser{
constructor(age,name){
this.age=age;
this.name=name;
}
set name(val){
console.log(name)
obfunction(name,val)
name=val;
}
}
//借助proxy reflect实现
class TargetObj {
constructor(age, name) {
this.name = name;
this.age = age;
}
}
let targetObj = new TargetObj(1, 'Martin');
let observerProxy = new Proxy(targetObj, {
set(target, property, value, reciever) {
if (property === 'name') {
observer(target[property], value);
}
Reflect.set(target, property, value, reciever);
}
}); // 定义值改变时的处理函数
function observer(oldVal, newVal) { // 其他处理逻辑...
console.info(`name属性的值从 ${oldVal} 改变为 ${newVal}`);
}
observerProxy.name = 'Lucas';
console.info(targetObj);