proxy 特性简单示例

2019-10-22  本文已影响0人  无名程序猿
    const target = {};
    // const obj = {};
    const handler = {
      get: function(target, key, receiver) {
        console.log(`geting${key}`);
        return Reflect.get(target, key, receiver);
      }
      // set: function(target, key, value, receiver) {
      //   console.log(`seting${key}`);
      //   return Reflect.set(target, key, value, receiver);
      // }
    };
    const obj = new Proxy(target, handler);
    // Object.setPrototypeOf(obj, proxy);
    obj.a = 1;
    obj.b = 2;
    console.log(obj.a);
    console.log(obj.b);
    // console.log(target.a);
    // console.log(target.b);
上一篇下一篇

猜你喜欢

热点阅读