Created
September 5, 2017 06:43
-
-
Save sriharshaj/111b8bcbd23a325be1e76d5cdb06968a to your computer and use it in GitHub Desktop.
Create custom iterator for objects
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
'use strict'; | |
let idMaker = { | |
[Symbol.iterator](){ | |
let nextId = 8000; | |
return { | |
next(){ | |
return { | |
value: nextId++, | |
done: false | |
} | |
} | |
} | |
} | |
} | |
let next = idMaker[Symbol.iterator]().next; | |
console.log(next()); | |
for (let v of idMaker){ | |
if(v > 8002){ | |
break; | |
} | |
console.log(v); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment