Skip to content

Instantly share code, notes, and snippets.

@Jks15063
Last active August 29, 2015 14:24
Show Gist options
  • Save Jks15063/e684cb66bbde00fc4e44 to your computer and use it in GitHub Desktop.
Save Jks15063/e684cb66bbde00fc4e44 to your computer and use it in GitHub Desktop.
var obj = { foo: "bar", wut: "what" };
var arr = [1,2,3,4,5];
obj[Symbol.iterator] = function() {
let keys = Object.keys(this);
let nextIndex = 0;
return {
next() {
return nextIndex < keys.length ?
{ value: keys[nextIndex++], done: false } :
{ value: void 0, done: true };
}
};
};
arr[Symbol.iterator] = function() {
var nextIndex = 0;
return {
next() {
return nextIndex < this.length ?
{ value: this[nextIndex++], done: false } :
{ value: void 0, done: true };
}
};
};
console.log([...obj]);
for(let o of obj) {
console.log('=', o);
}
console.log([...arr]);
for(let x of arr) {
console.log('-', x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment