Created
February 9, 2018 12:53
-
-
Save bhaskarmelkani/c70675679b7021cb593fa5c228cffa0d to your computer and use it in GitHub Desktop.
This file contains 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 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