Below code looses context of this when I am debouncing a object method. I want to handle this scenario where the returned function can access the name.
// function should return debounced function with this context
function debounce(cb, delay = 100) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(cb.bind(this, ...args), delay);
};
}
let x = {
name: "Priyadarshi",
sayHi: function () {
console.log("Hi " + this.name);
}
};
// Here returned function looses the this context
const y = debounce(x.sayHi);
document.addEventListener("mousemove", () => {
y();
});