How can I call an object method inside setTimeout
without losing the this
binding?
const bob = {
name: 'Bob',
greet: function () {
console.log(`Hi, I'm ${this.name}`)
}
};
bob.greet(); // Hi, I'm Bob
setTimeout(bob.greet, 500); // Hi, I'm
I know that in the setTimeout
call, this
is bound to the Window object.