Skip to content

Instantly share code, notes, and snippets.

@bhaskarmelkani
Created February 9, 2018 12:53
Show Gist options
  • Save bhaskarmelkani/c70675679b7021cb593fa5c228cffa0d to your computer and use it in GitHub Desktop.
Save bhaskarmelkani/c70675679b7021cb593fa5c228cffa0d to your computer and use it in GitHub Desktop.
var myArray = [1,2,3];
// with `for of`
for(var value of myArray) {
console.log(value);
}
// without `for of`
var _myArray = myArray[Symbol.iterator]();
while(var _iteration = _myArray.next()) {
if (_iteration.done) {
break;
}
var value = _iteration.value;
console.log(value);
}
/**
----------------------------------------------
*/
class Collection {
*[Symbol.iterator]() {
var i = 0;
while(this[i] !== undefined) {
yield this[i];
++i;
}
}
}
var myCollection = new Collection();
myCollection[0] = 1;
myCollection[1] = 2;
for(var value of myCollection) {
console.log(value); // 1, then 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment