on May 29, 2025 i took...

JavaScript method interceptor

function sum(...nums) {
  return nums.reduce((a, c) => a + c, 0);
}

function intercept(fn, { beforeFn, afterFn }) {
  return new Proxy(fn, {
    apply(target, thisArg, ...args) {
      beforeFn.apply(thisArg, args);
      const anyReturn = target.apply(thisArg, args);
      afterFn.apply(thisArg, args);
      return anyReturn;
    },
  });
}

const interceptedSum = intercept(sum, {
  beforeFn: (nums) => console.log("first", nums[0]),
  afterFn: (nums) => console.log("all", ...nums),
});

interceptedSum();