Uncaught TypeError:无法读取Object.toggleCompleted上未定义的属性“ completed”

时间:2019-09-29 21:36:46

标签: javascript function methods

我正在尝试找出“ 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');

0 个答案:

没有答案