> For the complete documentation index, see [llms.txt](https://radlohead.gitbook.io/typescript-deep-dive/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://radlohead.gitbook.io/typescript-deep-dive/recap/closure.md).

# 클로저

자바스크립트에서 얻은 가장 좋은 점은 클로저였습니다. 클로저는 외부 변수에도 스코프 밖에서 접근할 수 있게 해줍니다. 클로저는 사용하는 가장 좋은 방법을 설명합니다.

```typescript
function outerFunction(arg) {
    var variableInOuterFunction = arg

    function bar() {
        console.log(variableInOuterFunction) // Access a variable from the outer scope
    }

    // Call the local function to demonstrate that it has access to arg
    bar()
}

outerFunction('hello closure') // logs hello closure!
```

내부 함수가 외부 스코프의 변수에 접근할 수 있음을 예제를 통해 알 수 있습니다. 외부 함수의 변수는 내부 함수에 의해서만 접근이 가능합니다. 그러므로 클로저라는 용어로 사용되고 그 자체로 개념은 꽤 직관적입니다.

중요한 부분: 내부 함수는 외부 함수가 반환한 후에도 외부 스코프의 변수에 접근할 수 있습니다. 이것은 내부 함수가 외부 변수에 여전히 묶여있고 외부 함수에 의존적이지 않기 때문입니다. 다시 예제를 보겠습니다.

```typescript
function outerFunction(arg) {
    var variableInOuterFunction = arg
    return function() {
        console.log(variableInOuterFunction)
    }
}

var innerFunction = outerFunction('hello closure!')

// Note the outerFunction has returned
innerFunction() // logs hello closure!
```

## 클로저가 엄청난 이유

그것은 객체를 쉽게 구성할 수 있도록 하고 모듈 패턴으로 구성됩니다.

```typescript
function createCounter() {
    let val = 0
    return {
        increment() {
            val++
        },
        getVal() {
            return val
        }
    }
}

let counter = createCounter()
counter.increment()
console.log(counter.getVal()) // 1
counter.increment()
console.log(counter.getVal()) // 2
```

높은 수준에서 Node.js와 같은 것을 만들 수 있습니다.🌹

```typescript
// Pseudo code to explain the concept
server.on(function handler(req, res) {
    loadData(req.id).then(function(data) {
        // the `res` has been closed over and is available
        res.send(data)
    })
})
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://radlohead.gitbook.io/typescript-deep-dive/recap/closure.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
