Reputation: 299
I just saw this tutorial about debounce, but very confused about how the name arguments is passed through all these functions in debounce when he use debounceSayHello("Jeremy")
. Why debounce(sayHello,3000)
instead of debounce(()=>sayHello(name),3000)
? And when define the inner returned anonymous function, there's no arguments there, how did the 'Jeremy' is passed in and finally get to the apply function? Thank you so much!
function debounce (func, delay) {
let timerId;
return function () {
if (timerId) {
clearTimeout(timerId)
}
timerId=setTimeout(()=>func.apply(this,[...arguments]),delay);
}
}
function sayHello(name){
console.log(`Hello ${name}`);
}
let debouncedSayHello=debounce(sayHello,3000);
debouncedSayHello('Jeremy')
//Hello Jeremy
Original:
Upvotes: 0
Views: 98
Reputation: 19
Arguments is an Array-like object accessible inside function that contains the values of the arguments passed to that function.
function sumTotal(){
console.log(arguments); // {'1':1,'2':2,'3':3,'4': 4}
var numSet = [...arguments];
return numSet.reduce((total, num)=>total+num);
}
console.log('Sum: ', sumTotal(1,2,3,4));//Sum: 10
Upvotes: 1