There are two errors in your code:
this, in your function next, is an iterator, not an instance myDogs.
dog- This is both the key in your iteration and the result. This means dog never undefined at the end of the iteration. You should probably be more careful with the semantics of your variable names.
The solution to these two problems gives the following:
var myDogs = function(dogs) {
this.dogs = dogs;
let _this = this;
this[Symbol.iterator] = function() {
let i = -1;
return {
next() {
i++;
let j = 0;
var dog = void(0);
for (var dogName in _this.dogs) {
if (j == i) {
dog = { dog:dogName, hungry: _this.dogs[dogName] };
break;
}
j++;
}
var done = !dog;
return { value: dog, done };
}
};
};
};
But there is a simpler solution:
var myDogs = function(dogs) {
this[Symbol.iterator] = function() {
let i = -1;
return {
next() {
i++;
var dog = Object.keys(dogs)[i];
if (!dog) return {done:true};
return {value:{ dog, hungry:dogs[dog] }, done:false};
}
};
};
};
: void(0) .