es6语法中Proxy和Reflect对比的示例分析
这篇文章主要介绍es6语法中Proxy和Reflect对比的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
曲阳ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
如下所示:
{ //原始对象 let obj={ time:'2017-03-11', 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]; } }, // 拦截key in object操作 has(target,key){ if(key==='name'){ return target[key] }else{ return false; } }, // 拦截delete deleteProperty(target,key){ if(key.indexOf('_')>-1){ 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') } }); console.log('get',monitor.time); //2018-03-11 //操作 monitor.time='2018'; monitor.name='mukewang'; console.log('set',monitor.time,monitor);//2018-03-11;{time: "2017-03-11", name: "mukewang", _r: 123} console.log('has','name' in monitor,'time' in monitor);//true;false delete monitor.time; console.log('delete',monitor);//{time: "2017-03-11", name: "mukewang", _r: 123} // delete monitor._r; console.log('delete',monitor);//{time: "2017-03-11", name: "mukewang"} console.log('ownKeys',Object.keys(monitor));//["name", "_r"] } { let obj={ time:'2017-03-11', name:'net', _r:123 }; console.log('Reflect get',Reflect.get(obj,'time'));//get 2017-03-11 Reflect.set(obj,'name','mukewang'); console.log(obj);//{time: "2017-03-11", name: "mukewang", _r: 123} console.log('has',Reflect.has(obj,'name'));//true }
以上是“es6语法中Proxy和Reflect对比的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
当前名称:es6语法中Proxy和Reflect对比的示例分析
文章来源:http://azwzsj.com/article/ghpjjo.html