我正在尝试找出“ toggleCompleted”方法出了什么问题。错误状态:
'Uncaught TypeError: Cannot read property 'completed' of undefined
at Object.toggleCompleted (script.js:23)
var todoList = {
todos: [],
displayTodos: function () {
console.log('My Todos:', this.todos);
},
addTodo: function(todoText) {
this.todos.push({
todoText: todoText,
completed: false
});
this.displayTodos();
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText;
this.displayTodos();
},
deleteTodo: function(position) {
this.todos.splice(position, 1);
this.displayTodos();
},
toggleCompleted: function(position) {
var todo = this.todos[position]; // This is the line that is giving me issues
todo.completed = !todo.completed;
this.displayTodos();
}
};
我希望对象显示两件事:函数todoList.toggleCompleted
允许我插入所需的任何字符串,并且当我输入字符串时,completed属性显示为true。
我想知道如何解决该问题。我这样调用方法:todoList.toggleCompleted('hello there');