new Proxy(target,handle)

  1. const obj = {a:1}
  2. const handle = {
  3. "get": function (oTarget, sKey) {
  4. //可以拿到参数做一些处理
  5. // 默认行为,返回该值
  6. return oTarget[sKey];
  7. },
  8. "set": function (oTarget, sKey, vValue) {
  9. console.log(oTarget, sKey, vValue)
  10. // 默认赋值行为
  11. oTarget[sKey]= vValue;
  12. // 返回true表示成功
  13. return
  14. },
  15. // "deleteProperty": function (oTarget, sKey) {
  16. // if (sKey in oTarget) { return false; }
  17. // return oTarget.removeItem(sKey);
  18. // },
  19. // "enumerate": function (oTarget, sKey) {
  20. // return oTarget.keys();
  21. // },
  22. // "ownKeys": function (oTarget, sKey) {
  23. // return oTarget.keys();
  24. // },
  25. // "has": function (oTarget, sKey) {
  26. // return sKey in oTarget || oTarget.hasItem(sKey);
  27. // },
  28. // "defineProperty": function (oTarget, sKey, oDesc) {
  29. // if (oDesc && "value" in oDesc) { oTarget.setItem(sKey, oDesc.value); }
  30. // return oTarget;
  31. // },
  32. // "getOwnPropertyDescriptor": function (oTarget, sKey) {
  33. // var vValue = oTarget.getItem(sKey);
  34. // return vValue ? {
  35. // "value": vValue,
  36. // "writable": true,
  37. // "enumerable": true,
  38. // "configurable": false
  39. // } : undefined;
  40. // },
  41. }
  42. const proxyObj = new Proxy(obj,handle)

配合 reflect一起使用效果更佳