0

I am confused about how the pointer event is passed to the debounce() in addEventListener(). For the constant variable multiply it takes the two arguments which are passed as args in debounce(). However, when it is used in .addEventListener() I only see the syntax that defines the function. So how does the pointer event is passed as an argument like the first example?

function debounce(func, timeout = 1000) {
  let timer;
  return (...args) => {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      func(...args);
    }, timeout);
  };
}

const multiply = debounce((x, y) => console.log(x * y));
multiply(10, 10); //I see that this takes the arguments that assign to x and y 
 
button.addEventListener(
  "click",
  debounce((e) => console.log("clicked")));
//but here I am confused how the pointer event is being passed as an argument
D Park
  • 504
  • 10
  • 19
  • It's received a callback as first parameter `e` is the event object of button element that includes the info of current element has attached event – nart Feb 21 '22 at 07:14
  • _“I only see the syntax that defines the function”_ — You mean `debounce((e) => console.log("clicked"))`, right? Yes, this results in a function, as is expected for the second argument of `addEventListener`. `addEventListener` calls the function with the argument. See [Where do the parameters in a javascript callback function come from?](/q/34624634/4642212) and look up how higher-order functions work in JS. – Sebastian Simon Feb 21 '22 at 07:14

0 Answers0