객체 리터럴에서 This
생성자함수에서 this
전역에서 this를 쓰면 window 객체
this 바인딩은 함수의 호출 방식에 따라 동적 결정
// 이러한 함수가 있다고할때
const foo = function () {
console.dir(this)
}
1. 일반함수
foo() // this는 window
2. 메서드
const obj = { foo }
obj.foo() // this는 obj객체
3. 생성자 함수 호출
new foo() // this는 foo {} 인스턴스
4. 메서드 간접호출
const bar = { name: 'bar }
foo.call(bar) // bar
foo.bind(bar)() // bar
foo.apply(bar) // bar