拦截对象

4年以前  |  阅读数:354 次  |  编程语言:JavaScript 
//利用proxy拦截对象
let obj = { name: '', age: '', sex: '' }
let handler = {
get(target, key, receiver) {
    console.log("get", key); 
return Reflect.get(target, key, receiver);
  },
set(target, key, value, receiver) {
    console.log("set", key, value); // set name 李四  // set age 24
return Reflect.set(target, key, value, receiver);
  }
};
let proxy = new Proxy(obj, handler);
proxy.name = "李四";
proxy.age = 24;

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8