Hey campers,
let person = {
fname: 'Chandler',
lname: 'Bing'
};
person[Symbol.iterator] = function() {
let properties = Object.keys(person);
let count = 0;
let isDone = false;
let next = () => {
if (count >= properties.length)
isDone = true;
return { done: isDone,
value: this[properties[count++]]}
}
return {next}
}
console.log(person[Symbol.iterator]().next()) // {done: false, value: "Chandler"}
console.log(person[Symbol.iterator]().next()) // {done: false, value: "Chandler"}
console.log(person[Symbol.iterator]().next()) // {done: false, value: "Chandler"}
this doesn’t work, the next method is supposed to iterate over person
if the bottom is changed this works however
var it = person[Symbol.iterator]()
console.log(it.next()) // {done: false, value: "Chandler"}
console.log(it.next()) // {done: false, value: "Bing"}
console.log(it.next()) // {done: true, value: undefined}
shouldn’t these be doing the exact same thing?