{ let obj = { time: '2017-08-12', name: 'net', _r: 123 }; let monitor = new Proxy(obj, { get(target, key) { return target[key].replace('2017', '2018'); }, set(target, key, value) { if (key === 'name') { return target[key] = value; } else { return target[key]; } }, has(target, key) { if (key === 'name') { return target[key]; } else { return false; } }, deleteProperty(target, key) { if (key.indexOf('_') > -1) { delete target[key]; return true } else { return target[key] } }, ownKeys(target) { return Object.keys(target).filter(item => item != 'time') } }); console.log('get', monitor.time); monitor.time = '2018'; monitor.name = 'newname'; console.log('set', monitor.time, monitor.name); console.log('has', 'name' in monitor, 'time' in monitor) delete monitor.time; console.log('deleteProperty', monitor); delete monitor._r; console.log('deleteProperty', monitor) console.log('ownKeys', Object.keys(monitor)) }
|