23.proxy.html 940 B

12345678910111213141516171819202122232425262728293031323334
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // proxy 代理 在对象外面包了一层拦截器
  11. // const xxx = new Proxy(目标对象,拦截器)
  12. const obj = { name: "图图", age: 3 };
  13. const newProxy1 = new Proxy(obj, {
  14. get(target,key) {
  15. console.log(target,'target');
  16. console.log(key,'key')
  17. return target[key]
  18. },
  19. set(target,key,value) {
  20. console.log(target,'target1');
  21. console.log(key,'key1')
  22. console.log(value,'value1');
  23. target[key] = value;
  24. return true;
  25. }
  26. });
  27. newProxy1.name = '喜羊羊';
  28. console.log(newProxy1.name)
  29. </script>
  30. </body>
  31. </html>