Created
February 6, 2015 06:03
-
-
Save georgeOsdDev/b307288ceec69f194246 to your computer and use it in GitHub Desktop.
ES6 generator lesson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var genGen = function(arr, loop){ | |
return (function* (){ | |
var idx = 0; | |
var len = arr.length | |
while(true){ | |
if (len === idx && loop) idx = 0; | |
yield arr[idx++]; | |
} | |
})(); | |
}; | |
var arr = ["一","二","三","四","五"], | |
loop = true | |
; | |
var kanji = genGen(arr, loop); | |
kanji.next().value; // => "一" | |
kanji.next().value; // => "二" | |
kanji.next().value; // => "三" | |
kanji.next().value; // => "四" | |
kanji.next().value; // => "五" | |
// if loop is true | |
// kanji.next().value; // => "一" | |
// else | |
// kanji.next().value; // => undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment