apply()

102 阅读1分钟

apply()

  • apply()捕获器会在调用函数时中被调用。对应的反射 API 方法为 Reflect.apply().

    const myTarget = {};
    const proxy = new Proxy(myTarget, {
      apply(target, thisArg, ...arguments) {
        console.log("apply()");
        return Reflect.apply(...arguments);
      },
    });
    
  1. 返回值
    返回值无限
  2. 拦截的操作
    proxy(...argumentsList)
    Function.prototype.apply(thisArg, argumentsList)
    Function.prototype.call(thisArg, ...argumentsList)
    Reflect.apply(target, thisArgument, argumentsList)
    
  3. 捕获器程序处理参数
    target: 目标对象;
    thisArg: 调用函数时的 this 参数;
    argumentsList: 调用函数时的参数列表;
  4. 捕获器不变式 target 必须是一个函数对象