Class # this

달다로·2024년 6월 4일

JS

목록 보기
10/26
post-thumbnail

📌실행 컨텍스트 (맥락)


this

  • 하나의 객체이다.
    • 일반함수 : 호출위치에 따라 this 를 정의함
    • 화살표함수 : 자신이 선언된 함수 범위에서 this 를 정의함
    • const a = {} → 참조
  • window 와 같다.
    아무것도 없을때 (전역공간) 윈도우와 같다.
    this === window
  • 실행 맥락 (excute context) 으로 this 를 연결한다.

JS

const obj = {
	id: 1,
 	getId: function() {
  		return this.id;
  }
}
//매소드에 소유객체가 있으면 obj 는 this 가 된다.
console.log(this.id);
console.log(obj.id);
console.log(obj.getId();
  • 실행시에 소유자 확인이 안되면 this 는 전역 객체 에 연결된다.

JS

function caller(fn) {
	console.log('caller ==> ', fn())
}
const obj = {
	id: 1,
  getId: function() {
  	return this.id;
  }
}
console.log('this.id ==> ', this.id)
console.log(this.id);
console.log(obj.getId();
            
caller(obj.getId)
profile
나이들어서 공부함

0개의 댓글