Skip to content

Instantly share code, notes, and snippets.

@nathanhannig
Created August 22, 2017 21:18
Show Gist options
  • Save nathanhannig/b4276956a2f56901947b31aa23d1ffa2 to your computer and use it in GitHub Desktop.
Save nathanhannig/b4276956a2f56901947b31aa23d1ffa2 to your computer and use it in GitHub Desktop.
Iteration & Iterable Protocol Quiz
const james = {
name: 'James',
height: `5'10"`,
weight: 185,
[Symbol.iterator]: function() {
const object = this;
const keys = Object.keys(object);
let index = 0;
return {
next: () => {
const key = keys[index];
index++;
return { value: object[key], key: key, done: !(index < keys.length) };
}
};
}
};
let iterator = james[Symbol.iterator]();
console.log(iterator.next().value); // 'James'
console.log(iterator.next().value); // `5'10`
console.log(iterator.next().value); // 185
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment