您好,登錄后才能下訂單哦!
這篇文章主要介紹es6語法中Proxy和Reflect對比的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
如下所示:
{ //原始對象 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對比的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。