일반(normal) 함수와 화살표(arrow) 함수는 this
를 정의하는 범위가 다르다.
this.name = "Hi";
const beApro = {
name: "Kim",
normal: function () {
console.log(this.name);
},
arrow: () => {
console.log(this.name);
},
};
beApro.normal(); // Kim을 출력
beApro.arrow(); // Hi 출력
일반 함수는 호출 위치에 따라서 this
를 정의한다. 일반함수는 함수를 선언할 때, 호출 방식에 따라 this
에 바인딩되는 객체가 동적으로 결정된다.
화살표 함수는 자신이 선언된 함수 범위에서 this
를 정의한다. 간단히 말하자면 화살표 함수의 this
는 언제나 사위 스코프의 this
를 가리킨다는 의미이다.