JS: Generatorlar | Generators in Javascript | yield #kody_az
Dərs haqqında
JavaScript-də "generatorlar" xüsusi bir funksiya növüdür ki, bir neçə dəfə dayandırıla və yenidən başlana bilər. Generatorlar, digər funksiyalardan fərqli olaraq, tam icra edilən zaman birdəfəlik nəticə vermir; əksinə, onlar icra zamanı istənilən yerdə dayandırıla bilər və növbəti icra zamanı həmin yerdən davam etdirilə bilər.
Generator funksiyaları xüsusi olaraq function* sözü ilə təyin edilir və yield açar sözü istifadə olunur. yield ifadəsi generatorun dayandığı yerdəki dəyəri qaytarır və icranı dayandırır. Növbəti dəfə next() metodu çağırıldıqda, icra dayandığı yerdən davam edir.
next() metodu: Generatorun icrasını dayandığı yerdən davam etdirmək üçün istifadə olunur. next() metodu obyekt qaytarır ki, bu obyektdə value (gələn dəyər) və done (icranın tamamlanıb-tamamlanmaması) sahələri mövcuddur.
Generatorlar, böyük verilənləri təxminən bir-birini çəkmək və ya hesablama tələb edən prosesləri fasilələrlə həyata keçirmək üçün əlverişlidir. Bu, yaddaşdan daha səmərəli istifadə etmək və lazımsız hesablamalardan qaçmaq üçün istifadə olunur.
====
In JavaScript, "generators" are a special type of function that can be paused and resumed, making them unique compared to regular functions that run to completion in one go. Generators do not return a single value when called; instead, they generate a series of values over time, allowing for complex iteration processes.
Generators are defined using the function* syntax and utilize the yield keyword. The yield expression pauses the function, returning a value, and allows the function to be resumed from where it left off.
next() method: This method is used to resume the generator's execution from where it was paused. The next() method returns an object with two properties: value (the value yielded) and done (a boolean indicating if the generator has completed).
Generators are particularly useful for working with large data sets or sequences where you only need one item at a time, or for managing complex processes that require pauses, like asynchronous operations. This makes them a powerful tool for memory-efficient computation and on-demand value generation.
#kody_az #kodyaz #js #javascript #nodejs #es6 #viral #viralcode #viralvideo #random #code #coder #azerbaycan #azerbaijan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz