Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
Since March 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Experimental: 이 기능은 실험적인 기능입니다.
프로덕션 환경에서 사용하기 전에 브라우저 호환성 표를 주의 깊게 확인하세요.
Iterator 인스턴스의 take() 메서드는 이 반복자에서 주어진 숫자만큼의 요소를 산출하고 종료하는 새로운 반복자 헬퍼를 반환합니다.
순회 시작점부터 취할 요소의 수.
새로운 반복자 헬퍼를 반환합니다. 반환된 반복자 헬퍼는 원래 반복자의 요소들을 하나씩 산출하고, limit 개수만큼의 요소가 산출되었거나 원래 반복자가 소진되었을 때, 둘 중 먼저 발생하는 경우 완료됩니다 (next() 메서드가 { value: undefined, done: true }를 생성합니다).
정수로 변환시 limit가 NaN 되거나 음수일 경우에 발생합니다.
다음 예제는 피보나치 수열의 항을 생성하는 반복자를 들고, 처음 세 항을 출력합니다.
take()은 반복자를 직접 이동시키지 않을 때 가장 편리합니다. 반복자도 순회 가능하기 때문에, 반환된 헬퍼를 for...of 루프로 반복할 수 있습니다.
fibonacci()는 무한 반복자이기 때문에 직접적으로 순회시키기 위해 for 루프를 사용할 필요가 없습니다.
반복자의 일부를 얻기 위해 take()과 Iterator.prototype.drop()을 같이 사용할 수 있습니다.
limit가 음수거나 NaN일 경우 RangeError가 발생합니다.
limit가 반복자가 생성할 수 있는 총 요소 수보다 큰 경우(Infinity와 같이), 반환된 반복자 헬퍼는 기본적으로 원래 반복자와 같이 행동합니다.
| ECMAScript® 2027 Language Specification # sec-iterator.prototype.take |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2024년 7월 29일 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.