Created
January 4, 2015 02:17
-
-
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.
This file contains hidden or 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
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