என்பது முறை கொண்ட ஒரு பொருளாகும் இது ஐ வழங்குகிறது. ஒரு பொருள் ஆகும் அது முறை கொண்டிருந்தால் — அதுவே மற்றும் spread இயங்க வைப்பது. () என்பது iterator கள் உருவாக்க ஒரு வசதியான வழியாகும் இது உடன் .
என்பது முறை கொண்ட ஒரு பொருளாகும் இது ஐ வழங்குகிறது. ஒரு பொருள் ஆகும் அது முறை கொண்டிருந்தால் — அதுவே மற்றும் spread இயங்க வைப்பது. () என்பது iterator கள் உருவாக்க ஒரு வசதியான வழியாகும் இது உடன் .
next(){ value, done }[Symbol.iterator]for...offunction*yieldfunction* idGenerator() {
let id = 1;
while (true) { // infinite, but lazy — only computes on demand
yield id++; // pause here, return a value, resume on next()
}
}
const gen = idGenerator();
gen.next().value; // 1
gen.next().value; // 2 — execution resumed where it paused
மূல ধারணை: செயல்படுத்தல் ஒவ்வொரு yield இல் இடைநிறுத்த அடுத்த .next() இல் மீண்டும் தொடர முறைகளுக்கு இடையே உள்ளীட நிலையைக் காத்து கொள்ளும். நீங்கள் கேட்கும் வரை எதுவும் கணக்கிடப்படாது.
// process a huge/infinite sequence without building it all in memory
function* take(iterable, n) {
let i = 0;
for (const x of iterable) {
if (i++ >= n) return;
yield x;
}
}
[...take(idGenerator(), 3)]; // [1, 2, 3] — from an infinite generator
const range = {
from: 1, to: 3,
*[Symbol.iterator]() { for (let i = this.from; i <= this.to; i++) yield i; },
};
[...range]; // [1, 2, 3] — works with for...of and spread
Generator கள் மெதுவான/முடிவில்லாத வரிசைமுறைகள், தனிப்பயன் iteration, மற்றும் நினைவகம் திறன்மிக்க streaming ஐ செயல்படுத்த உதவுகின்றன.
அவை ஆரம்ப async நூலகங்களின் (co) அடிப்படையாகவும் இருந்தன மற்றும் async iteration (for await...of) ஐ அடிப்படையாக கொண்டிருக்கின்றன.
Iterator protocol என்பது for...of, spread, மற்றும் destructuring ஐ arrays, Maps, Sets, மற்றும் strings முழுவதும் கூட்டுவது.