Javascript wrapped function and this keyword

70 Views Asked by At

I'm learning about "bind", and I found there is another way of using the wrap function, without using "bind". I can code like below.

let user = {
  firstName: "John",
  sayHi() {
    alert(`Hello, ${this.firstName}!`);
  }
};


//using wrap function instead of bind 
setTimeout(function() {
  user.sayHi(); // Hello, John!
}, 1000);

If you use setTimeout(user.sayHi, 1000); instead of wrap function, it doen't work. I know why. But I can't understand, why does it work when you use wrap function?

0

There are 0 best solutions below