Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
22. this
✏️ 기억에 남는 내용
this
는 자신이 속한 객체 또는 자신이 생성할 인스턴스를 가리키는 자기 참조 변수(self-referencing variable)다.this
를 통해 자신이 속한 객체 또는 자신이 생성할 인스턴스의 프로퍼티나 메서드를 참조할 수 있다.this
는 자바스크립트 엔진에 의해 암묵적으로 생성되며, 코드 어디서든 참조할 수 있다.this
가 가리키는 값, 즉this
바인딩은 함수 호출 방식에 의해 동적으로 결정된다.객체 리터럴의 메서드 내부에서의
this
는 메서드를 호출한 객체를 가리킨다.생성자 함수 내부의
this
는 생성자 함수가 생성할 인스턴스를 가리킨다.this
는 객체의 프로퍼티나 메서드를 참조하기 위한 자기 참조 변수이므로 일반적으로 객체의 메서드 내부 또는 생성자 함수 내부에서만 의미가 있다.함수 호출 방식과
this
바인딩this
에는 전역 객체(global object)가 바인딩된다.this
에는 전역 객체가 바인딩된다.this
에 전역 객체가 바인딩된다.this
에는 메서드를 호출한 객체가 바인딩된다.this
는 메서드를 소유한 객체가 아닌 메서드를 호출한 객체에 바인딩된다는 것이다.this
에는 생성자 함수가 (미래에) 생성할 인스턴스가 바인딩된다.this
에 바인딩한다.this
로 사용할 객체를 전달하면서 함수를 호출하는 것은 동일하다.this
와 메서드 내부의 중첩 함수 또는 콜백 함수의this
가 불일치하는 문제를 해결 하기 위해 유용하게 사용된다.📝 간단한 퀴즈
함수 호출 방식에 따라
this
바인딩이 동적으로 결정되는 방법을 간단히 정리하시오.Function.prototype.apply/call/bind 메서드의 대표적인 사용 용도를 간단히 서술하시오.
일반적으로 클래스 기반의 언어들에서
this
는 언제나 클래스가 생성하는 인스턴스를 가리킨다. 하지만 자바스크립트의 경우 함수가 호출되는 방식에 따라this
가 가리키는 값이 바뀌게 된다. 왜 자바스크립트의this
는 이렇게 동작하는 걸까? 프로토타입과 관련지어 그 이유를 생각해보고 간단히 서술해보자.🧑🏻💻 연관 코드
// 관련 코드를 적어주세요.