简体   繁体   English

javascript:未定义的变量

[英]javascript: undefined variable

my variable 'todoList' is showing up as undefined. 我的变量“ todoList”显示为未定义。 i am very new to javascript and programming in general. 我对javascript和一般编程非常陌生。 any help would greatly be appreciated! 任何帮助将不胜感激!

 var todoList = { todos:[], displayTodos: function() { if (this.todos.length === 0) { console.log ('Your todos 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; } } this.displayTodos; } }; 

JavaScript always returns something . JavaScript总是返回某些内容 If I copy/paste your code into the Chrome console and run it, it tells me the returned result of running that code is undefined . 如果我将代码复制/粘贴到Chrome控制台中并运行,它会告诉我运行该代码的返回结果是undefined That's okay, since you're just setting up the object and have no intention of using the return value. 没关系,因为您只是设置对象而无意使用返回值。

在此处输入图片说明

After this, the object has been created and typing it into the Chrome console tells us that the variable todoList is an object. 此后,已经创建了对象,然后将其输入到Chrome控制台即可告诉我们变量todoList是一个对象。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM