# 12.1 이터레이션 프로토콜

* 모든 객체를 이터러블(iterable) 객체로 바꿀 수 있음

### 1) 이터레이터를 가져와서 이터레이터 프로토콜을 구현하는 방법

```javascript
  class Log {
    constructor(){
      this.messages = [];
    }

    add(message){
      this.messages.push({ message, timestamp: Date.now() });
    }

    [Symbol.iterator](){
      return this.messages.values();
    }
  }

  const log = new Log();
  log.add("first day at see");
  log.add("spotted whale");
  log.add("spotted another vessel");

  for (let entry of log) {
    console.log(`${entry.message} @ ${entry.timestamp}`);
  }
```

결과

```bash
first day at see @ 1535419744779
spotted whale @ 1535419744779
spotted another vessel @ 1535419744779
```

### 2) 이터이터를 직접 만드는 방법

```javascript
  class Log {
    constructor(){
      this.messages = [];
    }

    add(message){
      this.messages.push({ message, timestamp: Date.now() });
    }

    [Symbol.iterator](){
      let i = 0;
      const messages = this.messages;
      return {
        next() {
          if (i >= messages.length)
            return { value: undefined, done: true };
          return { value: messages[i++], done: false };
        }
      }
    }
  }
```


---

# Agent Instructions: 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:

```
GET https://april.gitbook.io/learning-js/chapter-12./12.1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
