javascript - 使用类的待办事项列表
问题描述
所以我正在学习 JS 课程,我一开始就面临一个问题,我无法在我的列表中添加任何待办事项。我找不到解决方案我做错了什么。我的代码:
<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
是一个类;你试图把它当作一个函数来调用。
相反,创建一个新实例TodoAddCommand
并将事件传递给构造函数,然后调用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>
推荐阅读
- sql - 如何在postgresql中获得没有不必要区域的大小
- python - Python multiprocessing module freeze_support() line can obmitted error Windows
- tensorflow - 我可以在 Keras 中循环定义模型吗?
- angular - 无法通过 POSTMAN 访问 Angular 路由
- python - 双向 LSTM 模型尚未建立错误
- java - 具有多个资源和 else 语句的 try-with-resources JDBC
- sql - 如何在加入后对多个列进行区分,然后为每个组排序并选择最新的?
- javascript - Leaflet.js 循环点击事件处理程序
- c - Of the given sites, incorrect result being returned
- c++ - 如何结束用户输入数组