new Proxy(target,handle)
const obj = {a:1}const handle = {"get": function (oTarget, sKey) {//可以拿到参数做一些处理// 默认行为,返回该值return oTarget[sKey];},"set": function (oTarget, sKey, vValue) {console.log(oTarget, sKey, vValue)// 默认赋值行为oTarget[sKey]= vValue;// 返回true表示成功return},// "deleteProperty": function (oTarget, sKey) {// if (sKey in oTarget) { return false; }// return oTarget.removeItem(sKey);// },// "enumerate": function (oTarget, sKey) {// return oTarget.keys();// },// "ownKeys": function (oTarget, sKey) {// return oTarget.keys();// },// "has": function (oTarget, sKey) {// return sKey in oTarget || oTarget.hasItem(sKey);// },// "defineProperty": function (oTarget, sKey, oDesc) {// if (oDesc && "value" in oDesc) { oTarget.setItem(sKey, oDesc.value); }// return oTarget;// },// "getOwnPropertyDescriptor": function (oTarget, sKey) {// var vValue = oTarget.getItem(sKey);// return vValue ? {// "value": vValue,// "writable": true,// "enumerable": true,// "configurable": false// } : undefined;// },}const proxyObj = new Proxy(obj,handle)
配合 reflect一起使用效果更佳
