Skip to content

Instantly share code, notes, and snippets.

@vdel26
Last active February 2, 2016 16:58
Show Gist options
  • Save vdel26/855c700cc8c55ef2952a to your computer and use it in GitHub Desktop.
Save vdel26/855c700cc8c55ef2952a to your computer and use it in GitHub Desktop.
ES5 vs ES6 Object iterator implementation
// ES5
function iterator (obj) {
var len = Object.keys(obj).length;
var i = 0;
var _it = {};
_it.next = function next () {
if (i < len) {
var key = Object.keys(obj)[i++];
return { value: [key, obj[key]], done: false };
}
else return { value: void(0), done: true };
}
return _it;
}
// ES6
function* iterator (obj) {
yield* Object.keys(obj).map(key => [key, obj[key]]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment