<aside> 💡 this는 함수 호출하는 방식, 즉 함수가 어떻게 호출되었는지에 따라 결정된다

</aside>

  1. 메서드 호출
  2. 일반 함수 호출
  3. 생성자 함수 호출
  4. apply/call/bind 메서드에 의한 간접 호출

1. 메서드 호출

this는 myObject를 가리킨다 ⇒ 메소드를 호출한 객체가 바인딩된다.

const myObject = {
  value: 10,
  show() {
    console.log(this.value);
  }
}
myObject.show(); // 10

2. 일반 함수 호출

일반함수로 호출하면 함수 내부의 this에는 전역 객체(window)가 바인딩된다.

function show() {
  this.value = 1;
  console.log(this);
}
show(); // window
const myObject = {
  value: 1,
  show1() {
    console.log(this.value); // 1

    function show2() {
      console.log(this.value); // ??
    }
    show2();
  }
};

myObject.show1();