Last active
January 31, 2018 03:14
-
-
Save alexmasyukov/2bfd421deef3d24eb01dbbc1cefed3d4 to your computer and use it in GitHub Desktop.
Iterators ES6
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
const arr = [1,2,3]; | |
arr.map((i, item) => { | |
console.log(item) | |
}); | |
arr.forEach((item, i) => console.log(item, i)) | |
for (let item of arr) { | |
console.log(item); | |
} | |
for (let char of 'Hello') { | |
console.log(char) | |
} | |
let types = new Set(['foo', 'tree', 'one']); | |
for(let type of types) { | |
console.log(type); | |
} | |
let obj = { | |
foo: 'bar', | |
baz: 'aux' | |
}; | |
for (let key of Object.keys(obj)) { | |
console.log(key); | |
} | |
let iterator = { | |
[Symbol.iterator]: function() { | |
return this; | |
}, | |
next: function() { | |
return { value: 'hey', done: false } | |
// всегда возвращает это | |
} | |
} | |
const interator1 = iterator[Symbol.iterator](); | |
console.log( interator1.next() ); | |
function createIterator(arr) { | |
let index = 0; | |
return { | |
next: () => { | |
return index < arr.length | |
? { value: arr[index++], done: false } | |
: { done: true }; | |
} | |
} | |
} | |
const myIterator = createIterator(['hey', 'yo', 'ya']); | |
console.log(myIterator.next()) | |
console.log(myIterator.next()) | |
console.log(myIterator.next()) | |
console.log(myIterator.next()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment