2019-01-15 Javascript Proxy

proxy是es6里面标准化构造对象拦截器的类
使用方法

new Proxy(target, handler)

基本用法

let data = { 
  name: 'jack',
  age: 20
}
let handler = {
  get: function(target, key, proxy){
    const today = new Date(); 
    console.log(`GET request made for ${key} at ${today}`); 
    return Reflect.get(target, key, proxy);
  }
}
let p = new Proxy(p, handler)
console.log(p.name)

操作转发

let target = {}
let p = new Proxy({}, target)
p.a = 12
console.log(target.a) // 12

你可能感兴趣的:(2019-01-15 Javascript Proxy)