Last active
February 2, 2016 16:58
-
-
Save vdel26/855c700cc8c55ef2952a to your computer and use it in GitHub Desktop.
ES5 vs ES6 Object iterator implementation
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
// 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