In the previous lesson, we assigned a generator for an object with a separate command. This was our object:
let obj = {
a: 1,
b: 2,
c: 3,
};
This is how we set up the generator:
obj[Symbol.iterator] = function *() {
for (let key in this) {
yield obj[key];
}
}
In fact, we can rewrite our code in
another way with
computed property
:
let obj = {
a: 1,
b: 2,
c: 3,
[Symbol.iterator]: function *(){
for (let key in this){
yield this[key];
}
}
};
Let's check our loop:
for (let elem of obj) {
console.log(elem); // 1, 2, 3
}
Rewrite the solution to the task from the previous lesson with a computed property.