JavaScript - this

LANA·2020년 4월 15일
0

JavaScript

목록 보기
14/21
post-thumbnail

실행은 함수 scope단위로 됨. block 단위로 생기는 게 아님
this는 실행 context이기 때문에, 실행 시점이 중요하다

this는 5가지 pattern만 외워놓으면 된다(아래 이미지)

5 Patterns of Binding 'this'

  1. Global: window
  2. Function 호출: window - function의 부모가 window니까
  3. Method 호출: 부모 object - 자신의 직계 부모만 가져옴. 할머니 할아버지 X
  4. Construction mode (new 연산자로 생성된 function 영역의 this): 새로 생성된 객체
  5. .call or .apply 호출: call, apply의 첫번째 인자로 명시된 객체

3. Method invocation


답안: true/true
앞에(부모)가 같으면 다 true


4. Construction mode

constructor = 생성자 함수

이 때의 this는 instance가 됨
new 키워드를 써서 만드는 것: construction mode / OOP적 mode


이때의 this는 f


5. .call or .apply 호출: call, apply의 첫번째 인자로 명시된 객체 (제일 헷갈림)

bar.call()안에 있는게 this가 됨

apply/call 의 this는 obj가 됨
apply는 parameter에 배열을 넣어줘야 함.

쾌락코딩 - apply/call/bind 차이점

profile
Let's code like chord !

0개의 댓글