2016-11-15 95 views
0

我正在學習javaScript並開發一個todoList應用程序。我已經把所有東西放在一個名爲todoList的對象中,然後首先有一個存儲todoList任務的對象數組。每個對象都有兩個屬性,即(todo = text,status = boolean)。之後是一個名爲showTasks的函數來顯示todoList中的任務。在showTasks函數中有一個if語句來檢查todoList是否爲空。之後是for循環顯示數組中的所有任務。另一個if語句在for循環之後顯示任務的狀態。 addTask函數用於在列表中添加新任務,只添加沒有狀態的任務的文本(默認狀態爲false)。 editTaskdeleteTask函數是非常明顯的。最後一個名爲toggleStatus的函數是切換一個任務的狀態,它不工作,我不明白爲什麼。您正在創建一個新的task,那麼這裏改變其狀態函數在javaScript中不工作

toggleStatus: function(count) { 
    var task = this.tasks[count]; 
    this.tasks[count].status = !task.status; 
    this.showTasks(); 
} 
+1

你是如何調用'toggleStatus'?您是否在Dev Tools控制檯選項卡中遇到錯誤? –

+0

您可以請上傳html以及檢查toggleStatus函數被調用的位置 – GraveyardQueen

+0

我至今尚未創建任何html文件。我在console.log中調用了todoList.toggleStatus,當我這樣做時,它應該使用showTasks函數中提到的(X)來顯示該特定任務。但它出現像「()任務1」 –

回答

0

你在for循環if聲明有一個錯字:d

這是很好的語法:

if (this.tasks[i].status) { 

通過我會的方式像這樣寫道,原型更好,因爲你的對象沒有存儲與聲明的對象一樣多的函數,並且創建速度更快;)請參閱JS - Why use Prototype?

function TodoList() { 
 
    // Storing tasks 
 
    this.tasks = []; 
 
} 
 

 

 
TodoList.prototype = { 
 
    // Displaying tasks 
 
    showTasks: function() { 
 
     if (this.tasks.length === 0) { 
 
      console.log("There's no tasks in the list."); 
 
     } else { 
 
      console.log("My Tasks:"); 
 
      for (var i = 0; i < this.tasks.length; i++) { 
 
       if (this.tasks[i].status) { 
 
        console.log("(X)", this.tasks[i].todo); 
 
       } else { 
 
        console.log("()", this.tasks[i].todo); 
 
       } 
 
      } 
 
     } 
 
    }, 
 

 
    // Adding a new task 
 
    addTask: function(task) { 
 
     this.tasks.push({ 
 
      todo: task, 
 
      status: false 
 
     }); 
 
     this.showTasks(); 
 
    }, 
 

 
    // Editing an existing task 
 
    editTask: function(count, task) { 
 
     this.tasks[count].todo = task; 
 
     this.showTasks(); 
 
    }, 
 

 
    // Deleting a task 
 
    deleteTask: function(count) { 
 
     this.tasks.splice(count, 1); 
 
     this.showTasks(); 
 
    }, 
 
    
 
    // Changing status 
 
    toggleStatus: function(count) { 
 
     var taskStatus = !this.tasks[count].status; 
 
     this.tasks[count].status = taskStatus; 
 
     this.showTasks(); 
 
    } 
 
}; 
 

 
var t = new TodoList(); 
 
t.addTask("yowwww"); 
 
t.addTask("miaou"); 
 
t.editTask(1, "miawwwwwou"); 
 
t.toggleStatus(0)

+0

我需要todoList在一個對象中,但你把它變成了一個函數?這是讓我的代碼工作的唯一方法嗎? –

+1

對不起,當我發現這個類型的時候,這是令人興奮的:D –

+0

@WaqasArshi你的語法仍然有效,選擇Tigger獲得相同的語法。我只是在我的迴應中揭示另一種以更傳統的方式申報班級的方式。 –

0

試試這個在this.tasks像這樣:

toggleStatus: function(count) { 
    this.tasks[count].status = !this.tasks[count].status; 
    this.showTasks(); 
} 

編輯:

如發現由帕特里克·費雷拉。您還需要從改變showTasks

if (this.tasks.status === true) { 

要:

if (this.tasks[i].status === true) { 
+0

這將是'this.tasks [count] .status =!task.status;' – madhur

+0

@madhur,很好,趕上! – ppovoski

0

toggleStatus: function(count) { 
    var task = this.tasks[count]; 
    task.status = !task.status; 
    this.showTasks(); 
} 

你需要做的是改變存儲task的狀態

var todoList = { 

// Storing tasks 
tasks: [], 

// Displaying tasks 
showTasks: function() { 
    if (this.tasks.length === 0) { 
     console.log("There's no tasks in the list."); 
    } else { 
     console.log("My Tasks:"); 
     for (var i = 0; i < this.tasks.length; i++) { 
      if (this.tasks.status === true) { 
       console.log("(X)", this.tasks[i].todo); 
      } else { 
       console.log("()", this.tasks[i].todo); 
      } 
     } 
    } 
}, 

// Adding a new task 
addTask: function(task) { 
    this.tasks.push({ 
     todo: task, 
     status: false 
    }); 
    this.showTasks(); 
}, 

// Editing an existing task 
editTask: function(count,task) { 
    this.tasks[count].todo = task; 
    this.showTasks(); 
}, 

// Deleting a task 
deleteTask: function(count) { 
    this.tasks.splice(count,1); 
    this.showTasks(); 
}, 
// Changing status 
toggleStatus: function(count) { 
    var task = this.tasks[count]; 
    task.status = !task.status; 
    this.showTasks(); 
} 

}; 
+0

在你的解決方案中,'task'是未定義的。 – ppovoski

+0

是的,更正。 – Tigger

+0

比你的考慮,但它不工作。當我這樣做(todoList.toggleStatus)時,它應該使用(X)打印該任務,如showTask函數中所述。 –

0

試試這個:我建議你改變countindex

toggleStatus: function(index) { 
    this.tasks[index].status = !this.tasks[index].status; 
    this.showTasks(); 
} 
0

沒有通通過javascript參考,所以當你這樣做時

var task = this.tasks[count]; 

它只是製作this.tasks [count]的另一個副本,並且您正在更新此副本,實際值沒有更新。所以這是問題。解決的辦法是

this.tasks[count].status = !this.tasks[count].status; 

希望它可以幫助