So I am learning JS classes and I am facing already an issue at start, I can't add any todo to my list. I can't find solution what I'm doing wrong. My code:
<input type="text" id="addTodo" />
<button onClick="TodoAddCommand">
Add
</button>
let Todos = [
{
id: 0,
content: "test",
done: false
}
]
class TodoAddCommand {
constructor(e) {
this.event = e;
}
execute() {
const todo = {
id: Todos.lenght,
content: this.event.data,
done: false,
}
Todos.push(todo);
console.logo(Todos);
}
}
TodoAddCommand is a class; you are trying to call it like a function.
Instead, create a new instance of TodoAddCommand and pass the event to the constructor, then call execute():
let Todos = [{
id: 0,
content: "test",
done: false
}
]
class TodoAddCommand {
constructor(e) {
this.event = e;
}
execute() {
const todo = {
id: Todos.lenght,
content: this.event.data,
done: false,
}
Todos.push(todo);
console.log(Todos);
}
}
<input type="text" id="addTodo" />
<button onClick="new TodoAddCommand(event).execute()">
Add
</button>