this

김용현·2023년 3월 17일
0

JSP

목록 보기
2/5
post-thumbnail

1. this

일반(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를 가리킨다는 의미이다.

profile
BeAPro!!

0개의 댓글

관련 채용 정보