Proxy可以监听对象身上发生了什么事情,并在这些事情发生后执行一些相应的操作。一下子让我们对一个对象有了很强的追踪能力,同时在数据绑定方面也很有用处。
var peo = { name: ‘zhangsan‘, salary: 27 }; //interceptor 拦截 var interceptor = { set: function (recObj, key, value) { console.log(key, ‘is changed to‘, value); //name is changed to lisi recObj[key] = value; } }; //创建代理以进行侦听 proxyEngineer = new Proxy(peo, interceptor); //做一些改动来触发代理 proxyEngineer.name = ‘lisi‘;//控制台输出:salary is changed to 60 console.log(peo) //{name: "lisi", salary: 27}
时间: 2024-10-08 18:18:27