-
Notifications
You must be signed in to change notification settings - Fork 0
문제2 풀이 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lywoo00
wants to merge
1
commit into
main
Choose a base branch
from
feat/lyw-this-binding-02
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
문제2 풀이 #7
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| //❓ 답해야 하는 것 | ||
| //각 줄의 출력 결과 (1~5)는 무엇인가? | ||
| //왜 그렇게 되는지 this와 클로저 관점에서 설명하시오. | ||
| //특히 아래 4개 상황에서 this가 어떻게 바인딩되는지 설명하시오: | ||
| //일반 함수 호출, 메서드 호출, bind로 고정한 함수, this가 없는 상황에서의 화살표 함수 | ||
|
|
||
| var name = "Global"; | ||
|
|
||
| const counter = { | ||
| name: "Counter", | ||
| value: 0, | ||
| inc() { | ||
| this.value++; | ||
| return this.value; | ||
| }, | ||
| run() { | ||
| console.log("1:", this.inc()); // counter가 run을 호출 -> this는 counter, this가 inc를 호출 -> this는 counter = 출력 결과 1 | ||
|
|
||
| const fn1 = this.inc; | ||
| const fn2 = this.inc.bind(this); | ||
|
|
||
| setTimeout(function () { | ||
| console.log("2:", this.inc ? this.inc() : this.name); // setTimeout은 전역객체(window)를 참조 -> winsodw에 inc가 없음 = 출력 결과 'Global' | ||
| }, 0); | ||
|
|
||
| setTimeout(() => { | ||
| console.log("3:", this.inc()); // setTimeout은 전역객체(window)를 참조하지만 화살표함수에는 this가 없음 -> this는 함수를 감싸고있는 부모를 바라봄 -> 1번에서 vlaue++ 됨 -> 같은 객체를 참조 = 출력 결과 2 | ||
| }, 0); | ||
|
|
||
| const arr = [1, 2, 3].map(function (x) { | ||
| if (x === 2) { | ||
| console.log("4:", this.inc ? this.inc() : this.name); // map의 콜백함수는 전역객체(window)를 카리킴 = 출력 결과 'Global' | ||
| } | ||
| return x; | ||
| }); | ||
|
|
||
| (function () { | ||
| console.log("5:", this.inc ? this.inc() : this.name); // 즉시실행 함수는 this가 전역객체(window)를 카리킴 = 출력 결과 'Global' | ||
| })(); | ||
| }, | ||
| }; | ||
|
|
||
| counter.run(); | ||
| /* | ||
| 실행 순서 | ||
| 1: 1 | ||
| 4: Global | ||
| 5: Global | ||
| 2: Global | ||
| 3: 2 | ||
| */ | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.prototype.map의 콜백에서 this는 전역(window)이 아니라고 합니다..!
단지 “map 콜백은 기본적으로 this를 bind하지 않는다”가 맞는 표현이라고 하네용
map은 콜백 함수 내부에 this를 바인딩하지 않는다.
thisArg를 주지 않으면 strict mode에서는 undefined이고,
브라우저 non-strict에서는 undefined가 window로 변환된다.
그래서 window.name → "Global"이 출력된다.
라고 합니다..!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
결론: map이 setTimeout처럼 this를 바인딩하지 않는건지 알았는데
설정은 안했을 뿐이었다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리뷰 감사합니다^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
머지웨안해요?