🖥️FE🖥️

this, bind

들눈 2023. 4. 4. 00:08
let person = {
  name: 'wow',
  wow: function () {
    console.log(this); //name 이 wow로 출력됨.
    setTimeout(function () {
      console.log(this); //여기서는 this가 windwo로 설정되어있기에 bind를 활용해야함.
    }, 1000);
  },
};
person.wow();
let person = {
  name: 'wow',
  wow: function () {
    setTimeout(
      (function () {
        console.log(this); //여기서는 this가 windwo로 설정되어있기에 bind를 활용해야함.
      }).bind(this),
    1000);
  },
};
person.wow();
let person = {
  name: 'wow',
  wow: function () {
    function hey() {
      console.log(this);
    }
    setTimeout(hey.bind(this), 1000);
  },
};
person.wow();