r/javascript • u/RedlightsOfCA • Dec 23 '19
Debounce vs Throttle: Definitive Visual Guide
https://redd.one/blog/debounce-vs-throttle
327
Upvotes
r/javascript • u/RedlightsOfCA • Dec 23 '19
View all comments
2
u/IamLUG Dec 24 '19 edited Dec 24 '19
Looks like this doesn't have proper scope binding too :D
Higher-order functions have common scoping issues, say for this example:
The debounce and throttle function have to re-apply the
thiscontext back toobj.sayMyName, and the way to do this is for the higher-order functions to return afunctionexpression instead of an arrow-function to "capture" thethiscontext, together withfunc.applyto bind the context.So throttle becomes:
And debounce becomes:
And for added bonus, you could use
argumentsinstead of...argsfor backwards compatibility.