Skip to content

Instantly share code, notes, and snippets.

@georgeOsdDev
Created February 6, 2015 06:03
Show Gist options
  • Save georgeOsdDev/b307288ceec69f194246 to your computer and use it in GitHub Desktop.
Save georgeOsdDev/b307288ceec69f194246 to your computer and use it in GitHub Desktop.
ES6 generator lesson
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