const musicCollection = {
albums: [{
title: "Amends",
artist: "Grey Daze",
year: "2020"
},
{
title: "Here on Earth",
artist: "Tim McGraw",
year: "2020"
},
{
title: "Orquídeas",
artist: "Karly Loaiza",
year: "2024"
}
]
}
musicCollection[Symbol.iterator] = function() {
return {
current: 0,
to: this.albums.length,
next() {
return this.current < this.to ? {
done: false,
value: musicCollection[this.current++]
} : {
done: true
};
}
}
}
for (let album of musicCollection) {
console.log(`${album.title} - ${album.artist} (${album.year})`);
}
主页
/
user-650012