constructor(){
this.messages = [];
}
add(message){
this.messages.push({message, timestamp : new Date()});
}
[Symbol.iterator]() {
//return this.messages.values();
let i=0;
const messages = this.messages;
return {
next() {
if ( i >= messages.length)
return { value : undefined, done: true};
return { value: `N°${i} : ${messages[i].message} @ ${messages[i++].timestamp}`, done: false};
}
}
}
}
const log = new Log();
log.add("premier");
log.add("second");
console.log([...log]);
code source