Skip to content

Instantly share code, notes, and snippets.

@winsonwq
Created January 4, 2015 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winsonwq/ec7bd701622e324e70fe to your computer and use it in GitHub Desktop.
Save winsonwq/ec7bd701622e324e70fe to your computer and use it in GitHub Desktop.
Iterability [3] in ECMAScript 6 is one such customization. An object is iterable if it has a method whose key is the symbol (stored in) Symbol.iterator. In the following code, obj is iterable.
let obj = {
data: [ 'hello', 'world' ],
[Symbol.iterator]() {
const self = this;
let index = 0;
return {
next() {
if (index < self.data.length) {
return {
value: self.data[index++]
};
} else {
return { done: true };
}
}
};
}
};
for (let x of obj) {
console.log(x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment