Skip to content

Instantly share code, notes, and snippets.

@erralb
Created May 2, 2024 10:58
Show Gist options
  • Save erralb/8bc5bba8038b8d5dbbb81859f8bc90b4 to your computer and use it in GitHub Desktop.
Save erralb/8bc5bba8038b8d5dbbb81859f8bc90b4 to your computer and use it in GitHub Desktop.
// Version 4 - Avec une classe d'objet
class ToDo {
constructor(name = 'My todo list', items = []) {
this.name = name;
this.items = items;
}
display() {
console.log(this.name, this.items);
console.log('Items number: ', this.items.length, '\n');
}
add(item) {
this.items.push(item);
console.log('Item added: ', item);
this.display();
}
change(index, newItem) {
console.log('Item changed from: "', this.items[index], '" to: "', newItem, '"');
this.items[index] = newItem;
this.display();
}
remove(index) {
console.log('Item removed: ', this.items[index]);
this.items.splice(index, 1);
this.display();
}
}
let todo1 = new ToDo;
todo1.display();
todo1.add('item1');
todo1.add('item2');
todo1.add('item3');
todo1.change(0, 'item1 est changé');
todo1.remove(1);
let todo2 = new ToDo('My todo list 2', ['item4', 'item5', 'item6']);
todo2.display();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment