For ES6 (eleven) and the Proxy Reflect

The Proxy 

the let obj = { 
  Time: '2019-01-01' , 
  name: 'ronle' 
} 
the let Monitor = new new the Proxy (obj, {
   // intercept object attributes read 
  GET (target, Key) {
     return target [Key] .replace ( '2019', '2020' ) 
  }, 
  // intercept the object properties set 
  sET (target, key, value) {
     // only key equal to name only modified 
    IF (key === 'name' ) {
       return the Reflect. SET (target, Key, value) 
    } the else {
       return target [Key] 
    } 
  },
  // 拦截 key in object操作
  has (target, key) {
    if (key === 'name') {
      return target[key]
    } else {
      return false
    }
  },
  // 拦截delete
  deleteProperty (target, key) {
    if (key.indexOf('-')) {
      delete target[key]
      return true
    } else {
      return target[key]
    }
  },
  // 拦截Object.keys
  // Object.getOwnPropertySymbols
  // Object.getOwnPropertyNames
  ownKeys (target) {
    return Object.keys(target).filter(item => item !== 'time')
  }
})
// 2020-01-01
console.log('get', monitor.time)
monitor.time = '2030'
monitor.name = 'kaka'
// 只有name被修改  kaka
console.log('set', monitor, monitor.name)

console.log('has', 'name' in monitor, 'time' in monitor)
// // 删除掉日期
// delete monitor.time
// console.log('delete',Monitor) 

return key does not equal is not equal to the element of time//
the console.log ( 'ownKeys' , Object.keys (Monitor)) similar Reflect with the proxy agent, the method attributes are the same 
the let obj



 = { 
  Time: '2019-01-01' , 
  name: 'ronle' 
} 

the console.log (Reflect .get (obj, 'Time' )) 
Reflect.set (obj, 'name', 'Kaka' ) 
the console.log (obj) 
the console.log (Reflect.has (obj, 'name'))

 

Guess you like

Origin www.cnblogs.com/ronle/p/11563989.html