JavaScript

Iterators & iterables (JS)

Në shqip: Iteratorët në JavaScript

ShpjegimiSQ

Çdo gjë që punon me for...of (vargje, string-e, Map, Set) është „iterable“: ka një metodë [Symbol.iterator]() që kthen një objekt me next(). Mund t'i bësh edhe objektet e tua iterable.

EnglishEN

Anything that works with for...of (arrays, strings, Map, Set) is “iterable”: it has a [Symbol.iterator]() method returning an object with next(). You can make your own objects iterable too.

Si ta mendosh

Si një distributor biletash: shtyp butonin dhe del bileta tjetër.

Lexoje në anglisht

Like a ticket dispenser: press the button and out comes the next ticket.

Shembull kodijavascript

const it = [10, 20][Symbol.iterator]();
it.next();   // { value: 10, done: false }
it.next();   // { value: 20, done: false }
it.next();   // { value: undefined, done: true }

Terma të lidhur