> 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/main-1/statefulfunctions.md).

# 상태 저장 함수

다른 프로그래밍 언어에 흔한 기능 중 하나는 `static` 키워드를 사용하여 함수 내 변수의 *생명* (*스코프* 아님)을 함수 호출 이후로 연장시키는 기능입니다. 여기 이런 일을 하는 `C` 예제가 있습니다:

```c
void called() {
    static count = 0;
    count++;
    printf("Called : %d", count);
}

int main () {
    called(); // Called : 1
    called(); // Called : 2
    return 0;
}
```

JavaScript (또는 TypeScript) 에는 함수 정적 변수가 없기 때문에 로컬 변수를 감싸는 여러가지 추상화를 이용하여 같은 일을 수행합니다. 예를 들어 `class` 를 사용하면:

```typescript
const {called} = new class {
    count = 0;
    called = () => {
        this.count++;
        console.log(`Called : ${this.count}`);
    }
};

called(); // Called : 1
called(); // Called : 2
```

> C++ 개발자들도 그들이 `functor` 라 부르는 (`()` 연산자를 오버라이딩한 클래스) 패턴으로 동일한 일을 합니다.


---

# 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/main-1/statefulfunctions.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.
