我正在制作一个简单的登录页面,其中根据登录者返回博客。 当我设置react应用程序时,我以这种方式设置了登录处理程序。注意设置状态的顺序。
var todoList = {
todos: [],displayTodos: function() {
if (this.todos.length === 0) {
console.log('Your todo list is empty!');
} else {
console.log('My todos:');
for (var i = 0; i < this.todos.length; i++) {
if (this.todos[i].completed === true) {
console.log('(x)',this.todos[i].todoText);
} else {
console.log('( )',this.todos[i].todoText);
}
}
}
},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];
todo.completed = !todo.completed;
this.displayTodos();
},toggleAll: function() {
var totalTodos = this.todos.length;
var completedTodos = 0;
for (var i = 0; i < totalTodos; i++) {
if (this.todos[i].completed === true) {
completedTodos++;
}
}
}
if (completedTodos === totalTodos) {
for (var i = 0; i < totalTodos; i++) {
this.todos[i].completed = false;
}
} else {
for (var i = 0; i < totalTodos; i++); {
this.todos[i].completed = true;
}
}
this.displayTodos();
}
};
var displayTodosButton = document.getElementById('displayTodosButton');
displayTodosButton.addEventListener('click',function() {
todoList.displayTodos();
});
<h1>Todo List</h1>
<button id = "displayTodosButton">Display Todos</button>
<button id = "toggleAllButton">Toggle All</button>
这给了我错误:
TypeError:“ t为空”
然后当我像这样切换setUser(..)和setUserBlogs(..)的顺序时...
var todoList = {
todos: [],function() {
todoList.displayTodos();
});
有效。
为什么按此顺序工作而不按其他顺序工作?