-
Notifications
You must be signed in to change notification settings - Fork 0
Udemy/Ts/section5/61: this & binding #37
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
Merged
Merged
Conversation
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
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 작성한 커밋입니다.
Github에서 작성한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
✍Udemy/Ts/section5/61: this & binding
🔥KeyWord
📝Description
accountingCopy
라는 변수에 새로운 객체구조로 값들을 할당하고accounting.describe
로 접근을 하게 되면 의도하지 않은 error가 발생하게 된다. 물론 log는 출력되지만undefined
로 출력된다. 이유는 다음과 같다. 우선 undefined가 발생한 위치를 파악해야한다.우리가descrbie
프로퍼티를 불러온 대상은accountingCopy
이다. 즉,accountingCopy
는describe: accounting.describe
로accounting
이라는 객체에 접근한것 같지만 사실상 어느 무엇도 참조하지 못한다.describe
함수는 원래accounting
객체에서 정의된 것이지만accountingCopy
로 복사되었을 때describe
함수를 호출하는 객체는 이제accountingCopy
가 된다.this
는 함수가 호출된 시점의 객체를 참조하므로accountingCopy
에서describe
를 호출할 경우this
는accountingCopy
를 가리킨다. 그러나accountingCopy
는Department
클래스의 인스턴스가 아니므로this.name
은undefined
로 출력된다. 정확하게는 js의 동작원리로 바라본다면변수
를 할당하는스택
이 참조하기 위해서는힙
영역을 통해서 참조해야 하는데스택에서 스택을 참조하고 있기 때문에 서로 연결되지 않아 더미객체로 인지하고문제는 JavaScript에서 this가 함수 호출 방식에 따라 동적으로 바인딩되기 때문이다. 함수가 객체의 메서드로 호출될 때 this는 그 객체를 참조한다. 하지만 describe: accounting.describe로 메서드를 복사해 다른 객체(accountingCopy)에서 호출하면, this는 원래의 객체 accounting이 아닌 accountingCopy를 참조하려고 하며 accountingCopy는 Department 클래스의 인스턴스가 아니므로 this.name은 undefined가 된다. 이로 인해 undefined가 출력된다.undefined
를 출력하게 되는 불상사가 발생하는 것이다.accountingCopy
의describe
객체에()
가 없는accounting.describe
로 할당한 이유는 함수를 실행하지 않고 _함수의 참조만 전달_하기 위함이다.this
키워드를 사용해야 하는데 위 코드와 같이 매개변수를 통해this
로 청사진 class형식을 참조하게 하면서 새로 할당한 객체 name 을 참고하도록 유도해야undefiend
와 같은 결과를 회피 할 수 있다.이 문제를 해결하려면 this를 명시적으로 고정시켜야 한다. 이를 위해 **bind()**를 사용하여 this를 accounting 객체에 고정시킬 수 있다. 예를 들어 accountingCopy.describe = accounting.describe.bind(accounting);와 같이 작성하면, describe 메서드가 항상 accounting 객체를 참조하게 된다. 따라서 this.name은 accounting 객체의 name 속성을 참조하게 되어, undefined 대신 올바른 값이 출력된다.📌Summary
힙
과스택
부분을 조금 더 다루자면 위의 undefined의 오류가 발생하는 코드는힙
과스택
이 정상적으로 참조하고 동작하고 있다. 그렇기 때문에undefined
가 출력된다. 문제는 this이다. 함수는 정상적으로 힙 메모리를 참조할 수 있지만 this가 제대로 바인딩(binding)되지 않아 undefined가 발생하는 것이다. 따라 바인딩(binding)도 기회가 되면 다시 다루어보고 싶다. (바인딩(binding)을 간단히 설명하면 다음과 같다.특정 변수나 함수가 특정 객체 또는 값에 연결되는 것
)