JS Iterators प्रोटोकॉल
In this page:
const iterable = {
[Symbol.iterator]() {
return {
next() {
return { value: value, done: false };
}
};
}
};
Iterator क्या है?
एक iterator एक ऐसा object है जो एक-एक करके values देता है। इसमें एक next() method होता है।
Arrays, Maps, Sets, और strings सभी built-in iterables हैं, यही वह चीज़ है जो for...of और spread operator को इतनी अलग-अलग data structures पर एक जैसे तरीके से काम करने देती है।
उदाहरण: What Is an Iterator?
// Declare the constant `arr`, set to `[1, 2, 3]`
// Declare the constant `arr`, set to `[1, 2, 3]`
const arr = [1, 2, 3];
// Declare the constant `iterator`, set to `arr[Symbol.iterator]()`
// Declare the constant `iterator`, set to `arr[Symbol.iterator]()`
const iterator = arr[Symbol.iterator]();
// Print `iterator.next()` to the console
// Print `iterator.next()` to the console
console.log(iterator.next());
next() का Result
next() method value और done properties वाला एक object return करता है। iteration खत्म होने पर done true हो जाता है।
एक बार done true हो जाए, तो next() की आगे की calls को error देने या sequence को फिर से शुरू करने के बजाय { value: undefined, done: true } ही return करते रहना चाहिए।
उदाहरण: next() Result
const arr = ["a"];
const it = arr[Symbol.iterator]();
console.log(it.next()); // { value: "a", done: false }
console.log(it.next()); // { value: undefined, done: true }
Iterable Protocol
एक iterable object एक Symbol.iterator method देता है। Arrays, strings, और maps आम iterables हैं।
यह shared protocol ही है जो for...of, spread syntax, और destructuring को उस हर object पर काम करने देता है जो इसे लागू करता है, सिर्फ language के अपने built-in collections पर ही नहीं।
उदाहरण: Iterable Protocol
// Declare the constant `iterable`; its value is built below
// Declare the constant `iterable`; its value is built below
const iterable = {
[Symbol.iterator]() {
// Declare the variable `i`, set to `0`
// Declare the variable `i`, set to `0`
let i = 0;
// Return `{ next: () => i < 3 ? { value: i++, done: false } : { value: undefined, done: true } }` from this function
// Return `{ next: () => i < 3 ? { value: i++, done: false } : { value: undefined, done: true } }` from this function
return { next: () => i < 3 ? { value: i++, done: false } : { value: undefined, done: true } };
},
};
for (const v of iterable) console.log(v);
Custom Iterator
आप एक next() method define करके और value व done return करके अपना खुद का iterator बना सकते हैं।
यह वही underlying mechanism है जिसे generators अपने आप इस्तेमाल करते हैं — function* लिखना आपको बिना next()/done की bookkeeping खुद manually संभाले एक iterator दे देता है।
उदाहरण: Custom Iterator
// Define the function `makeIterator` taking `max`
// Define the function `makeIterator` taking `max`
function makeIterator(max) {
// Declare the variable `i`, set to `0`
// Declare the variable `i`, set to `0`
let i = 0;
// Return `{ next: () => i < max ? { value: i++, done: false } : { value: undefined, done: true } }` from this function
// Return `{ next: () => i < max ? { value: i++, done: false } : { value: undefined, done: true } }` from this function
return { next: () => i < max ? { value: i++, done: false } : { value: undefined, done: true } };
}
// Declare the constant `it`, set to `makeIterator(2)`
// Declare the constant `it`, set to `makeIterator(2)`
const it = makeIterator(2);
// Print `it.next(), it.next(), it.next()` to the console
// Print `it.next(), it.next(), it.next()` to the console
console.log(it.next(), it.next(), it.next());
Iterator बनाम Iterable
एक iterator में next() होता है। एक iterable में Symbol.iterator होता है जो एक iterator return करता है। कुछ objects दोनों हो सकते हैं।
यह फर्क जानना custom data structures बनाते समय मदद करता है: object पर खुद manually एक next() method expose करने के बजाय Symbol.iterator लागू करें (इसे iterable बनाते हुए)।
उदाहरण: Iterator vs Iterable
function* gen() { yield 1; }
const g = gen();
console.log(typeof g.next); // iterator: has next()
console.log(typeof g[Symbol.iterator]); // iterable: has Symbol.iterator too
next()सेdoneऔरvalueके बिना एक object return करना, इसलिए loops कभी नहीं रुकते।- किसी custom object पर
[Symbol.iterator]()भूल जाना, इसलिएfor...ofTypeError: not iterablethrow करता है। - किसी iterable को iterator से confuse करना, क्योंकि एक iterable एक iterator produce करता है और iterator के पास
next()होता है।
- Closures, hoisting, और
thisबताते हैं कि functions में scope और context कैसे काम करते हैं। call,apply, औरbindthisको control करते हैं, और IIFEs और strict mode यह shape करते हैं कि code कैसे चलता है।- Generators और iterator protocol control करते हैं कि values एक-एक करके कैसे produce होती हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: