this, binding

효딩딩·2022년 8월 24일
0

this

  • this는 현재 실행되는 코드의 실행 컨텍스트를 가리킨다.

this binding

  • this binding은 this에 실행 컨텍스트의 주체를 연결 짓는 것, 즉 this가 무엇을 가리킬지 연결하는 것
  • this binding은 실행 컨텍스트가 활성화 될 때 한다.
  • 실행 컨텍스트는 이 컨텍스트를 지닌 함수가 호출될 때 활성화 된다.
    - 실행 컨텍스트가 활성화될 때 (함수를 호출할 때) this가 binding 된다.
    - this는 함수를 호출할 때 정해진다. 함수를 어떻게 호출했냐에 따라 this는 달라진다.

this binding 5가지 케이스

  • 전역공간에서 : window / global
  • 함수 호출시 : windonw / global
  • 메소드 호출시 : 메소드 호출 주체 (메소드명 앞)
  • callback 호출시 : 기본적으로 함수 호출시와 동일
  • 생성자 함수 호출시 : 인스턴스

출처:
https://velog.io/@younoah/JS-Flow-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-this%EC%99%80-this-binding
https://www.codementor.io/@diegopalacios/what-s-the-binding-of-this-1agz39841z


(영문 해석)

this, binding

JavaScript's this keyword is one of the hardest aspects of the language to grasp. But it is critically important for writing more advanced JavaScript code.

In JavaScript, the this keyword allows us to:

  • Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword.
  • Identifying the object in the current execution context when we invoke a method.

The this keyword is very closely associated with JavaScript functions. When it comes to this, the fundamental thing is to understand where a function is invoked. Because we don't know what is in the this keyword until the function is invoked.

The usage of this can be categorized into five different binding aspects. In this article, we will learn about all five aspects with examples.

Source: https://www.freecodecamp.org/news/javascript-this-keyword-binding-rules/

profile
어제보다 나은 나의 코딩지식

0개의 댓글