Skip to content
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

[1주차][호준] 클로저 #108

Open
wants to merge 1 commit into
base: ganeodolu
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions JS/closure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 클로저

- 결론
- 외부함수보다 내부함수가 더 오래 유지될 때 내부함수는 생명주기가 종료한 외부함수의 변수를 참조할 수 있는데 이 내부함수를 클로저라고 부름
- 설명
- 상위스코프에 대한 참조는 함수가 실행된 위치가 아니라 **함수가 정의된 위치**에 의해 결정(렉시컬 스코프)
- 중첩함수가 상위 스코프의 식별자를 참조하고 있고 중첩 함수가 외부 함수보다 더 오래 유지되는 경우에 한정하는 것이 일반적임
- 상태가 의도치 않게 변경되지 않도록 안전하게 은닉하고 특정 함수에게만 상태 변경을 허용하여 상태를 안전하게 변경하고 유지하기 위해 사용

```jsx
const x = 1;

function outerFunc(){
const x = 10;
function innerFunc(){
console.log(x); // 10
}
innerFunc();
}

outerFunc();
```

- 요약
- 내부함수에서 생명주기가 끝난 외부함수의 변수를 참조할 수 있는 내부함수
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍 👍 👍

저는 이 한마디로 클로저를 요약할 수 있다면 클로저에 대한 이해가 끝난거라고 생각해요 👍 👍 👍 👍 👍

- 참고
- 모던 자바스크립트 Deep Dive