2016-04-22 22 views
0

my変数 'todoList'が未定義として表示されています。私は非常に一般的なJavaScriptとプログラミングの新機能です。どんな助けも大いに評価されるでしょう!javascript:未定義変数

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; 
 
    } 
 
};

+0

は宣言して割り当てるためのコードは 'todoList'は正常に見えます。あなたのコードのどこに変数が 'undefined'であるかについての詳細を提供する必要があります。 – smaili

+0

あなたの '' if(this.todos [i] .completed === true) ' –

+0

に' &&!this.todos [i] .todoText'を追加しようとすると、より高いスコープからtodoListオブジェクトにアクセスしようとしている可能性があります。 todoListは表示されないので、これは未定義です。 –

答えて

0

JavaScriptが常に何かを返します。 Chromeコンソールにコードをコピーして貼り付けて実行すると、そのコードを実行した結果の返信結果はで、定義されていないとと表示されます。これは大丈夫です。オブジェクトを設定するだけで、戻り値を使用するつもりはないからです。この後

enter image description here

、オブジェクトが作成されたとChromeのコンソールにそれを入力すると、変数todoListがオブジェクトであることを教えてくれるされています。

enter image description here

+0

ありがとう!これは本当に役に立ちました! – justinsunghokim

+0

うれしかったので、嬉しいです。あなたが私の答えに満足すれば、それを受け入れることができますか? –